Skip to content

Syntax basics

Davyd McColl edited this page Oct 14, 2017 · 3 revisions

Generally, with NExpect, you will expect something to be, equal, contain, or have something else. Your expectations start like:

Expect(foo).To
Expect(foo).To.Be
Expect(fooCollection).To.Contain

Some word-properties have been added simply as user-extension points, eg:

Expect(foo).To.Have
Expect(foo).To.Be.A
Expect(foo).To.Be.An

Negation can be handled in two ways:

Expect(foo).Not.To
Expect(foo).To.Not

NExpect isn't trying to be opinionated about grammar -- it's just trying to facilitate readable tests. Both read well, and NExpect will prevent silliness like:

Expect(foo).Not.To.Not // etc

Expectations should throw reasonable messages, eg:

Expect(1).To.Equal(2); // should throw with a message like "Expected 1, but got 2"

But the problem comes in when you have a bunch of expectations and just knowing that one failed on a mismatched value is not enough. So expectations should always allow an optional custom message:

Expect(person.Legs).To.Equal(2, "people should normally have two legs");
// in which case, you'll get your custom message first, followed by the value failure

But enough of this -- let's get on to Simple checks