-
Notifications
You must be signed in to change notification settings - Fork 0
Entity assertions
The FluentExpect API provides for asserting over whole entities. In other words, where property assertions deal with an entity's specific properties, entity assertions talk about the whole entity, itself. This page describes the available methods.
Note: contextual aliases (e.g., .that.[alias]) exist for each method, but functionally, the methods are the same. The aliases exist to make the API's grammar a little easier on the eyes. 😃
Compares the contextual value with an expected one. By default, the comparison is shallow and strictly equal (===).
Scope: Preserves/unchanged.
equal(expected: T, eqType: EqType = EqType.strictly): FluentMatcherCore<T, TParent>
Expect(returnVal).to.equal(3.14);
Expect(viewModel).to.have(p => p.prop).that.equals(3.14);
A number of helper methods exist mainly for readability:
strictlyEqual(expected: T): FluentMatcherCore<T, TParent>;
looselyEqual(expected: T): FluentMatcherCore<T, TParent>;
deeplyEqual(
expected: T,
eqType: EqType.strictly | EqType.loosely = EqType.strictly
): FluentMatcherCore<T, TParent>;
deepStrictlyEqual(expected: T): FluentMatcherCore<T, TParent>;
deepLooselyEqual(expected: T): FluentMatcherCore<T, TParent>;
EqType can be one of deepStrictly
, deepLoosely
, loosely
, or strictly
.
Checks whether the contextual value is defined.
Scope: Preserves/unchanged.
beDefined(): FluentMatcherCore<T, TParent>;
Expect(val).to.beDefined();
Check whether the contextual value matches the given regular expression.
Scope: haveMatches
optionally narrows the scope to the matching parts, while match
preserves it.
match(matcher: RegExp): FluentMatcherCore<string, TParent>;
haveMatches(matcher: RegExp): FluentMatcherCore<string[], TParent>;
Expect(val)
.to.match(/(\d+)-(\d+)-(\d+)/);
// or
Expect(val)
.to.haveMatches(/(\d+)-(\d+)/)
.that.has(parts => +parts[1])
.that.equals(3);
Checks whether the contextual value throws an error. The parameter errorType
is optional,
and, if provided, the assertion will fail if the thrown error isn't an instance of it.
Scope: Narrows to the actual error object for further matching.
throw(): FluentMatcherCore<Error, TParent>;
throw<TError extends Error>(errorType?: {
new(...args: Array<any>): TError;
}): FluentMatcherCore<TError, TParent>;
Expect(lambda)
.to.throw(MyError)
.with.properties({
message: /some regex/,
custom: c => Expect(c).to.beDefined() // ...
});
Checks whether the contextual value satisfies (causes to evaluate to true) the given predicate.
Scope: Preserves/unchanged.
satisfy(
predicate: (t: T) => boolean
): FluentMatcherCore<T, TParent>;
Expect(val).to.satisfy(v => complicatedBizConstraint(val));
Checks whether the contextual value is an instance of the expected type.
Scope: Preserves/unchanged.
beInstanceOf(expectedType: {
new(): any;
}): FluentMatcherCore<T, TParent>;
Expect(val).to.beInstanceOf(MyModel);
Existence-checks the selected property and narrows the fluent context to its value.
Scope: Narrows to the selected property's value.
have<R>(selector: (t: T) => R): FluentMatcherCore<R, T>;
Expect(myModel).to.have(p => p.prop).that.equals(42);