Question
Enables the Actor to query the system under test.
Direct Subclasses:
Indirect Subclasses:
Examples:
import { Actor, AnswersQuestions, UsesAbilities, Question } from '@serenity-js/core'
import { Ensure, equals } from '@serenity-js/assertions'
const LastItemOf = <T>(list: T[]): Question<T> =>
Question.about('last item from the list', (actor: AnswersQuestions & UsesAbilities) => {
return list[list.length - 1];
});
Actor.named('Quentin').attemptsTo(
Ensure.that(LastItemFrom([1,2,3]), equals(3)),
);
import { AnswersQuestions, UsesAbilities, Question } from '@serenity-js/core'
import { CallAnApi } from '@serenity-js/rest'
const TextOfLastResponseStatus = () =>
Question.about<number>(`the text of the last response status`, actor => {
return CallAnApi.as(actor).mapLastResponse(response => response.statusText);
});
import { Actor, AnswersQuestions, UsesAbilities, Question } from '@serenity-js/core'
import { CallAnApi, LastResponse } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions';
const RequestWasSuccessful = () =>
Question.about<number>(`the text of the last response status`, actor => {
return LastResponse.status().answeredBy(actor) === 200;
});
const actor = Actor.named('Quentin').whoCan(CallAnApi.at('https://myapp.com/api'));
actor.attemptsTo(
Send.a(GetRequest.to('/books/0-688-00230-7')),
Ensure.that(RequestWasSuccessful(), isTrue()),
);
Tests:
See also:
Static Method Summary
Static Public Methods | ||
public static |
Factory method that simplifies the process of defining custom questions. |
|
public static |
isAQuestion(maybeQuestion: any): boolean Checks if the value is a Question. |
Constructor Summary
Protected Constructor | ||
protected |
constructor(subject: string) |
Member Summary
Public Members | ||
public |
subject: * |
Method Summary
Public Methods | ||
public |
describedAs(subject: string): Question<T> Changes the description of this question's subject. |
|
public | ||
public |
toString(): string Describes the subject of this Question. |
Static Public Methods
public static about(description: string, body: function(actor: AnswersQuestions & UsesAbilities): R): Question<R> source
Factory method that simplifies the process of defining custom questions.
Params:
Name | Type | Attribute | Description |
description | string | ||
body | function(actor: AnswersQuestions & UsesAbilities): R |
Examples:
const EnvVariable = (name: string) =>
Question.about(`the ${ name } env variable`, actor => process.env[name])
Protected Constructors
protected constructor(subject: string) source
Params:
Name | Type | Attribute | Description |
subject | string | The subject of this question |
Public Members
public subject: * source
Public Methods
public describedAs(subject: string): Question<T> source
Changes the description of this question's subject.
Params:
Name | Type | Attribute | Description |
subject | string |
public map(mapping: function(value: A, index?: number): Promise<O> | O): Question<Promise<Mapped>> source
Creates a new Question, which value is a result of applying the mapping
function to the value of this Question.
Params:
Name | Type | Attribute | Description |
mapping | function(value: A, index?: number): Promise<O> | O | A mapping function that receives a value of type
|
Returns:
Question<Promise<Mapped>> | A new Question which value is a result of applying the
|
Examples:
import { Question, replace, toNumber } from '@serenity-js/core';
Question.about('the price of some item', actor => '$3.99')
.map(replace('$', ''))
.map(toNumber)
// => Question<Promise<number>>
// 3.99
import { Question, trim } from '@serenity-js/core';
Question.about('things to do', actor => [ ' walk the dog ', ' read a book ' ])
.map(trim())
// => Question<Promise<string[]>>
// [ 'walk the dog', 'read a book' ]
import { Question } from '@serenity-js/core';
Question.about('normalised percentages', actor => [ 0.1, 0.3, 0.6 ])
.map((actor: AnswersQuestions) => (value: number) => value * 100)
// => Question<Promise<number[]>>
// [ 10, 30, 60 ]
import { Question } from '@serenity-js/core';
import { LastResponse } from '@serenity-js/rest';
interface UserDetails {
id: number;
name: string;
}
LastResponse.body<UserDetails>().map(actor => details => details.id)
// => Question<number>
Tests:
- append
- normalize
- replace
- slice
- split
- toLocaleLowerCase
- toLocaleUpperCase
- toLowerCase
- toUpperCase
- trim
- Question when mapping the answer works with a static value
- Question when mapping the answer works with a promise
- Question when mapping the answer works with a static list of values
- Question when mapping the answer works with a promised list
- Question when mapping the answer works with a mappable collection (Array, ElementArrayFinder, etc.)
See:
- AnswerMappingFunction
- Mappable