【问题标题】:Mocha: Translating a user story to a spec of describe / it (bdd framework i.e. mocha)?Mocha:将用户故事翻译成描述/它的规范(bdd 框架,即 mocha)?
【发布时间】:2015-01-06 16:39:22
【问题描述】:

我正在尝试使用 describe 和 it 将我的用户故事转换/翻译成 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。

编辑

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

【问题讨论】:

  • 您可以使用cucumber.js 直接针对这些文件运行测试。
  • describe("withdraw cash"),与it("has sufficient funds")it ("doesn't have sufficient funds")。你可以写一个端到端的测试来运行整个链的任何函数。我不确定问题是什么;你有一个起点和终点。只需使用给定的起点测试终点的结果。如果您想知道它在哪里中断(如果它中断),只需为 TDD 编写另一套测试。我误解了这个问题吗?

标签: javascript node.js bdd mocha.js user-stories


【解决方案1】:

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

场景 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);

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

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

【讨论】:

    【解决方案2】:

    我参加聚会有点晚了,但以防其他人也有类似的需求。我有一个 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]);
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      相关资源
      最近更新 更多