摩卡:将用户故事翻译成描述/它的规范(BDD框架,即摩卡)

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

本文关键字:摩卡 BDD 框架 用户 故事 翻译 描述      更新时间:2023-09-26

我正在尝试使用描述和它将我的用户故事转换/翻译为摩卡规格。我发现它有点令人困惑。

举一个例子:

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

因此,使用摩卡的"描述"和"它",翻译它的最佳方式是什么。我看到的大多数示例都以测试函数和方法的方式使用 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 $100
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);

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

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

我参加聚会有点晚了,但以防其他人有类似的需求。我有一个摩卡库,它提供了一个非常丰富的小黄瓜,也就是功能/场景/给定/何时/然后等语法。您可以在此处找到该项目:

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]);
        });
    });
});