Skip to content
Chris Dibbern edited this page Apr 7, 2018 · 9 revisions

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. 😃

.to.equal

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>

Example

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>;

Comparison types

EqType can be one of deepStrictly, deepLoosely, loosely, or strictly.

.to.beDefined

Checks whether the contextual value is defined.

Scope: Preserves/unchanged.

beDefined(): FluentMatcherCore<T, TParent>;

Example

Expect(val).to.beDefined();

.to.match, .to.haveMatches

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>;

Example

Expect(val)
  .to.match(/(\d+)-(\d+)-(\d+)/);

// or

Expect(val)
  .to.haveMatches(/(\d+)-(\d+)/)
  .that.has(parts => +parts[1])
  .that.equals(3);

.to.throw

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>;

Example

Expect(lambda)
  .to.throw(MyError)
  .with.properties({
    message: /some regex/,
    custom: c => Expect(c).to.beDefined() // ...
  });

.to.satisfy

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>;

Example

Expect(val).to.satisfy(v => complicatedBizConstraint(val));

.to.beInstanceOf

Checks whether the contextual value is an instance of the expected type.

Scope: Preserves/unchanged.

beInstanceOf(expectedType: {
        new(): any;
    }): FluentMatcherCore<T, TParent>;

Example

Expect(val).to.beInstanceOf(MyModel);

.to.have

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>;

Example

Expect(myModel).to.have(p => p.prop).that.equals(42);