Coffeescript 如何在Mocha中实现addMatchers?

Coffeescript 如何在Mocha中实现addMatchers?,coffeescript,jasmine,mocha.js,chai,Coffeescript,Jasmine,Mocha.js,Chai,我正在使用CoffeScript书进行测试,该书教BDD使用Jasmine,但我使用的是Mocha/Chai,我遇到了以下代码: beforeEach -> @addMatchers toBeDiscounted: (orig,discount) -> actual = @actual @message = -> "Expected #{actual} to be #{discount}% of #{orig}"

我正在使用CoffeScript书进行测试,该书教BDD使用Jasmine,但我使用的是Mocha/Chai,我遇到了以下代码:

beforeEach -> 
  @addMatchers  
    toBeDiscounted: (orig,discount) ->  
      actual = @actual  
      @message = -> "Expected #{actual} to be #{discount}% of #{orig}"  
      actual is (orig * (1-(discount/100)))  
然后在测试中:

it "should persist the discount", ->  
  expect(test.basket.applyDiscount(10)).toBeDiscounted(50, 10)  

在摩卡/柴,我如何做到这一点?

茉莉花媒人相当于摩卡/柴土地上的柴帮手。
您将需要更新等级库辅助对象以包含一个。然后,您需要编写Chai Helper本身;使其能够添加
折扣
功能

spec/spec-helper.js

spec/helpers/discounted.js

然后,您应该能够执行以下操作:

expect(foo).to.be.discounted(50, 10)
对不起,我没有测试这个。另外,我很抱歉这是Javascript(不是Coffeescript)。。。但希望它能为您指明正确的方向。

请参阅本文
function discount(x, discount) {
  return x * (1 - (discount / 100));
}

module.exports = function(chai) {
  var Assertion = chai.Assertion;

  Assertion.addMethod('discounted', function (y, p) {
    var obj = this._obj;
    new Assertion(obj).to.be.a('number');
    this.assert(
        obj === discount(y, p)
      , "expected #{this} to be " + y + "% of " + p
      , "expected #{this} to not be " + y + "% of " + p
    );
  });
}
expect(foo).to.be.discounted(50, 10)