2014-01-09 36 views
0

失败我使用摩卡与chai.js为CoffeeScript的单元测试。我有一个咕task的任务来将咖啡文件编译到测试文件夹,并启动PhantomJS来运行摩卡测试。Chai.js不指定其断言在测试

一切正常,但是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的一个好办法发生了什么?

像:Asse田:预计真正是假符合预期(ARR [3])to.equal( “3”)

谢谢。

回答

1

如果没有人来了一个更复杂的解决方案,使chai.Assertion.includeStack工作很好地与CoffeeScript的代码,你总是可以传递消息给某些功能在expect接口:

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

"here"字符串是当断言失败时将与错误消息一起输出的消息。看看documentation,但不是所有的函数都带有可选的消息。我首先想使用上面to.be.truetrue方法不带一个可选的消息。

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

+0

谢谢,这将做到这一点。如果chai.js可以自动添加断言(这里只是'false'),那会很好。 –