Chai.js没有指定哪个断言在测试中失败

时间:2014-01-09 22:38:37

标签: unit-testing coffeescript mocha chai

我正在使用带有chai.js的mocha进行CoffeeScript单元测试。我有一个艰巨的任务是将咖啡文件编译到测试文件夹并启动PhantomJS来运行mocha测试。

一切正常但是chai.js只表示测试失败的内容以及预期和实际值是什么,它没有指定在测试用例中没有传递的断言。是否有任何好的方法来打印断言或至少是失败的断言的索引?我还打开了chai.Assertion.includeStack但是只显示了JavaScript文件中对CoffeeScript测试没有帮助的行号。

FizzBu​​zzTest.coffee

fizz = new FizzBuzz()

describe "Print numbers from 1 to 100", ->
    it "First 10 digits from 1 to 5", ->
        result = fizz.do()
        arr = result.split(fizz.Delimiter)
        expect(arr[0]).to.equal("1")
        expect(arr[1]).to.equal("2")
        expect(arr[2]).to.equal(fizz.Fizz)
        expect(arr[3]).to.equal("3") # this assertion should fail
        expect(arr[4]).to.equal(fizz.Buzz)

执行测试

$ grunt test

Running "mocha:run" (mocha) task
Testing: Content/runner.html

  Print numbers from 1 to 100
    ✓ First 10 digits from 1 to 5 
    1) Last 10 digits from 95 to 100
    ✓ Print FizzBuzz instead of number which is divisible by both 3 and 5 
    ✓ Check number 
    ✓ Check Fizz 
    ✓ Check Buzz 
    ✓ Check FizzBuzz 
    ✓ Check if > 100 then null 
    ✓ Check if < 1 then null 

  8 passing (113ms)
  1 failing

  1) Print numbers from 1 to 100 Last 10 digits from 95 to 100:
     AssertionError: expected true to be false
      at file:///Users/milan/Sites/CoffeeTests/Content/js-libs/chai/chai.js:918
      at file:///Users/milan/Sites/CoffeeTests/Content/js-libs/chai/chai.js:1159
      at file:///Users/milan/Sites/CoffeeTests/Content/js-libs/chai/chai.js:3563

>> 1/9 tests failed (0.11s)
Warning: Task "mocha:run" failed. Use --force to continue.

Aborted due to warnings.

问题: 有没有一种方法可以将chai.js设置为更具体地说明AssertionError发生的位置?

喜欢: AssertionError:期望true为false in line expect(arr [3])。to.equal(“3”)

谢谢。

1 个答案:

答案 0 :(得分:1)

如果没有人提出更复杂的解决方案来使chai.Assertion.includeStack与CoffeeScript代码很好地协作,您总是可以将消息传递给expect界面中的某些函数:

it("bluh", function () {
    chai.expect(false).to.be.equal(true, "here");
});

"here"字符串是一个消息,当断言失败时将输出错误消息。看一下documentation但是因为并非所有函数都接受可选消息。我首先想要使用上面的to.be.true,但true方法不会使用可选消息。

assert接口支持比expect接口更多功能的可选消息。

相关问题