Skip to content

String expectations

Davyd McColl edited this page Oct 16, 2017 · 1 revision

The usual suspects are here:

var theString = "the quick brown fox jumps over the lazy dog";
Expect(theString).To.Start.With("the quick");
Expect(theString).To.Contain("fox jumps");
Expect(theString).To.End.With("dog");

Plus a few others:

var theString = "the quick brown fox jumps over the lazy dog";
Expect(theString).To.Contain("fox").And("dog"); // passes
Expect(theString).To.Contain("dog").And("fox"); // also passes: .And doesn't care about order
Expect(theString).To.Contain("over").Then("lazy"); // passes: "lazy" is found after "over";
Expect(str).To.Start.With("the")
  .And.Contain("brown")
  .Then("jumps")
  .And.End.With("lazy dog");

As well as support for regular expressions:

Expect(theString).To.Be.Matched.By("^the.*dog$");
Expect(theString).To.Be.Matched.By(someRegexInstance);

And matcher functions:

Expect(theString).To.Match(s => s.Contains("quick"));

Matcher functions may not be the most eloquent way, but they do allow you easy control over more complex logic.