Mocha:将用户故事转化为 describe / it 规范(bdd 框架,即 mocha)?

Mocha: Translating a user story to a spec of describe / it (bdd framework i.e. mocha)?

我正在尝试使用 describe 和 it convert/translate 我的用户故事到 mocha 规范中。我发现它有点混乱。

举个例子:

Story: Account Holder withdraws cash

As an Account Holder
I want to withdraw cash from an ATM
So that I can get money when the bank is closed

Scenario 1: Account has sufficient funds
Given the account balance is $100
 And the card is valid
 And the machine contains enough money
When the Account Holder requests $20
Then the ATM should dispense $20
 And the account balance should be $80
 And the card should be returned

Scenario 2: Account has insufficient funds
Given the account balance is $10
 And the card is valid
 And the machine contains enough money
When the Account Holder requests $20
Then the ATM should not dispense any money
 And the ATM should say there are insufficient funds
 And the account balance should be $20
 And the card should be returned

所以使用 mocha 的 "describe" 和 "it",最好的翻译方式是什么。我见过的大多数示例都以测试功能和方法的方式使用 BDD - 这对我来说真的更像是 TDD。

编辑

我也想用模拟;如果我准确翻译用户故事,那么我可以模拟吗?

我对摩卡不是很熟悉所以我给你做了一个类似的。希望你明白了。

场景 1:

it should authenticate card currectly if a valid card and details are provided:
System.readCard(new Card({config}))....
expect(System.Card.isValid).toBe(true)

it should get the correct financial details from the user 
// You know it's your card and you have 0
var originalBalance = System.User.Balance;
expect(originalBalance).to.be(100);

it should have enough for the user
System.User.RequestBalance = 20;
expect(System.Vault.Balance).to.be.at.least(System.User.RequestBalance);

it should dispense the requested amount:
System.Dispence(System.User.RequestBalance);
expect(System.Vault.Balance).to.be(System.Vault.Balance - System.User.RequestBalance);

it should deduct the amount from user's account
expect(System.User.Balance).to.be(originalBalance - System.User.RequestBalance);

it should return the card
Syste.ejectCard();
expect(System.CardHolder.isEmpty).to.be(true);

.....等等。这只是给你一个想法的快速方法。

请注意,一般的想法是你做某事并对结​​果做出断言,然后检查是否发生了这件事,如果它发生了,就证明你想要的事情确实发生了。

我来晚了一点,但以防其他人有类似的需求。我有一个 mocha 库,它提供了非常丰富的 Gherkin aka Feature/Scenario/Given/When/Then 等语法。您可以在这里找到该项目:

https://github.com/dotnetprofessional/LiveDoc/tree/master/packages/livedoc-mocha

其中的一个示例如下所示:

feature(`Account Holder withdraws cash

    Account Holders should be able to withdraw cash at any of the
    companies ATMs.

    Rules:
    * Account Holders should have a valid keycard
    * Have sufficient available funds
    * The ATM has the necessary funds
    `, () => {

    scenario("Account has sufficient funds", () => {
        let atm = new ATM();
        let cashReceived: number;

        given(`the account holders account has the following:
        | account | 12345 |
        | balance | 100   |
        | status  | valid |
    `, () => {
                const accountHolder = stepContext.tableAsEntity;
                atm.setStatus(accountHolder.account, accountHolder.status);
                atm.deposit(accountHolder.account, accountHolder.balance)
            });

        and("the machine contains '1000' dollars", () => {
            atm.addCash(stepContext.values[0]);
        });

        when("the Account Holder requests '20' dollars", () => {
            cashReceived = atm.withDraw(scenarioContext.given.tableAsEntity.account, stepContext.values[0]);
        });

        then("the ATM should dispense '20' dollars", () => {
            cashReceived.should.be.equal(stepContext.values[0]);
        });

        and("the account balance should be '80' dollars", () => {
            atm.getBalance(scenarioContext.given.tableAsEntity.account).should.be.equal(stepContext.values[0]);
        });
    });
});