0

我刚刚了解了 serenity-js,并且正在尝试一下。我正在关注本教程并注意到以下示例:

james.attemptsTo(
    Start.withAnEmptyTodoList(),
    AddATodoItem.called('Buy some milk')
)

任务Start

export class Start implements Task {

    static withATodoListContaining(items: string[]) {       // static method to improve the readability
        return new Start(items);
    }

    performAs(actor: PerformsTasks): PromiseLike<void> {    // required by the Task interface
        return actor.attemptsTo(                            // delegates the work to lower-level tasks
            // todo: add each item to the Todo List
        );
    }

    constructor(private items: string[]) {                  // constructor assigning the list of items
    }                                                       // to a private field
}

我真的很喜欢这种语法,并希望通过更多起始场景继续此设置。实现这一目标的正确方法是什么?

4

1 回答 1

0

对于任何有同样问题的人,这就是我解决它的方法(通过 serenity-js 存储库找到了一个类似的设置):

// Start.ts
export class Start {
    public static withATodoListContaining = (items: string[]): StartWithATodoListContaining => new StartWithATodoListContaining(items);
}

// StartWithATodoListContaining.ts
export class StartWithATodoListContaining implements Task {

    static withATodoListContaining(items: string[]) {       
        return new StartWithATodoListContaining(items);
    }

    performAs(actor: PerformsTasks): PromiseLike<void> {    
        return actor.attemptsTo(                            
            // todo: add each item to the Todo List
        );
    }

    constructor(private items: string[]) {                  
    }                                                       
}
于 2019-03-09T07:33:57.140 回答