import {Ensure} from '@serenity-js/assertions'
public class | source

Ensure

Used to perform verification of the system under test.

Resolves any Answerable describing the actual state and ensures that its value meets the Expectations provided.

Extends:

Interaction → Ensure

Direct Subclasses:

src/Ensure.ts~EnsureOrFailWithCustomError

Examples:

Usage with static values
 import { actorCalled } from '@serenity-js/core';
 import { Ensure, equals } from '@serenity-js/assertions';

 const actor = actorCalled('Erica');

 actor.attemptsTo(
   Ensure.that('Hello world!', equals('Hello world!'))
 );
Composing expectations with `and`
 import { actorCalled } from '@serenity-js/core';
 import { and, Ensure, startsWith, endsWith } from '@serenity-js/assertions';

 const actor = actorCalled('Erica');

 actor.attemptsTo(
   Ensure.that('Hello world!', and(startsWith('Hello'), endsWith('!'))
 );
Overriding the type of Error thrown upon assertion failure
 import { actorCalled, TestCompromisedError } from '@serenity-js/core';
 import { and, Ensure, startsWith, endsWith } from '@serenity-js/assertions';
 import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest';

 const actor = actorCalled('Erica')
     .whoCan(CallAnApi.at('https://example.com'));

 actor.attemptsTo(
   Send.a(GetRequest.to('/api/health')),
   Ensure.that(LastResponse.status(), equals(200))
     .otherwiseFailWith(TestCompromisedError, 'The server is down, please cheer it up!')
 );

Tests:

Static Method Summary

Static Public Methods
public static

that(actual: Answerable<T>, expectation: Expectation<any, A>): Ensure<A>

Constructor Summary

Public Constructor
public

constructor(actual: Answerable<T>, expectation: Expectation<T>)

Method Summary

Public Methods
public

otherwiseFailWith(typeOfRuntimeError: Function, message: string): Interaction

Overrides the default AssertionError thrown when the actual value does not meet the expectations set.

public

performAs(actor: UsesAbilities & CollectsArtifacts & AnswersQuestions): Promise<void>

Makes the provided Actor perform this Interaction.

public

toString(): string

Generates a description to be used when reporting this Activity.

Protected Methods
protected

Maps an Outcome to AssertionError.

protected

Maps an ExpectationOutcome to appropriate RuntimeError.

Static Public Methods

Public Constructors

public constructor(actual: Answerable<T>, expectation: Expectation<T>) source

Params:

NameTypeAttributeDescription
actual Answerable<T>
expectation Expectation<T>

Public Methods

public otherwiseFailWith(typeOfRuntimeError: Function, message: string): Interaction source

Overrides the default AssertionError thrown when the actual value does not meet the expectations set.

Params:

NameTypeAttributeDescription
typeOfRuntimeError Function

The type of RuntimeError to throw, i.e. TestCompromisedError

message string

The message explaining the failure

Returns:

Interaction

public performAs(actor: UsesAbilities & CollectsArtifacts & AnswersQuestions): Promise<void> source

Makes the provided Actor perform this Interaction.

Params:

NameTypeAttributeDescription
actor UsesAbilities & CollectsArtifacts & AnswersQuestions

Returns:

Promise<void>

public toString(): string source

Generates a description to be used when reporting this Activity.

Returns:

string

Protected Methods

protected asAssertionError(outcome: Outcome): AssertionError source

Maps an Outcome to AssertionError.

Params:

NameTypeAttributeDescription
outcome Outcome

Returns:

AssertionError

protected errorForOutcome(outcome: ExpectationOutcome): RuntimeError source

Maps an ExpectationOutcome to appropriate RuntimeError.

Params:

NameTypeAttributeDescription
outcome ExpectationOutcome

Returns:

RuntimeError