-
Notifications
You must be signed in to change notification settings - Fork 0
Home
The fluent assertions plugin for the Alsatian xUnit framework provides a way to string assertions
together with the possibility of narrowing the assertion scope, allowing you to communicate test
assertions clearly and concisely. The default API in Alsatian begins with Expect
, but this library
uses Assert
to avoid confusion.
To help you choose which API you want to use, please take a look at the examples on this page.
Here's an example of fluency and scope-narrowing:
Assert(viewModel)
.is(SomeClass)
.has(o => o.prop)
.that.hasMatch(/\d+/) // narrow scope (that) to prop, then match
.that.converted(Number).equals(7);
Here's what that would look like in Alsatian's default Expect API:
Expect(viewModel instanceof SomeClass).toBeTruthy();
Expect(viewModel.prop).toBeDefined();
let regex = /\d+/;
Expect(viewModel.prop).toMatch(regex);
Expect(viewModel.prop.match(regex)[0]).toEqual(7);
Here's another way to write the first example. This way is more useful when you need to assert over multiple properties on viewModel
:
const viewModel = new SomeClass();
Assert(viewModel)
.is(SomeClass)
.hasProperties({
prop: p => Assert(p).hasMatch(/\d+/)
.that.converted(Number).equals(7)
});
(P.S. There are a number of ways to make nested property assertions. For clarity, the above version is somewhat verbose.)
Downsides to using our fluent API:
- The API's natural language grammar can sound a bit unnatural, at times, when compared to the Expect grammar. This is due to the API's ability to string assertions together more "fluently." :-)
- We haven't yet implemented support for Alsatian property and method spies. You'd have to use the default Expect framework, for the moment (you can use both, at the same time).
Note that, at the fluent level, the assertion scope can narrow to individual properties via the 'that' operator, and they can broaden via the 'kThx' operator not unlike the push and pop stack operations.
Assert({ a: "b", c: "d", 3: 1415 })
.has(o => o.a).that.equals("b").kThx
.has(o => o.c).that.equals("d").kThx
.has(o => o[3]).that.equals(1415);
Please see kThx and that for more information and examples.
Beyond that, you can also break the assertion scope apart by assigning it to a variable:
let a = Assert(viewModel).has(v => v.someProp);
a.that.has(p => p.itsOwnProp);
a.that.has(p => p.someOther).that.equals(3);
Or, you can use properties matchers:
Assert(viewModel)
.hasAsserts({
someProp: {
itsOwnProp: a => a.isDefined(),
someOther: 3
}
});