2013-09-23 69 views
4

我正在運行使用摩卡的單元測試,而不是顯示記者摩卡在第一個錯誤崩潰的所有拋出的斷言錯誤。有什麼建議麼?AssertionError崩潰摩卡

我在崩潰得到的錯誤是這樣的:

/Users/Robert/Code/JRJ/Server/node_modules/chai/lib/chai/assertion.js:106 
     throw new AssertionError(msg, { 
      ^
AssertionError: expected 200 to equal 202 
npm ERR! weird error 8 
npm ERR! not ok code 0 

這是相同的,無論我是否使用柴或內置的斷言庫。我跑摩卡使用此命令(我npm test運行它):

mocha --reporter 'spec' --recursive 

而且我使用的庫版本是這些:

  • 節點:0.10.18
  • 摩卡咖啡:1.12。 0
  • 釵:1.8.0
  • HAPI:1.10.0

測試代碼:

var hapi = require('hapi'), 
     expect = require('chai').expect, 
     assert = require('assert'); 

    describe("Customer API", function(){ 
     var server = require('../../../../src/apis/customer'); 

     //works as expected 
     describe('simpleExample', function(){ 
     it("should cause a test failure", function(done){ 
      expect(200).to.equal(202); 
      done(); 
     }); 
     }); 

     //crashes Mocha 
     describe('Authentication', function(){ 
     it('Should get user token', function(done){ 
      server.inject("/[email protected]&password=testa", function(res){ 
      expect(res.statusCode).to.equal(202); //returns 200, crashes Mocha (the expected 202 is intentional to cause an assertion error) 
      //assert.ok(res.statusCode === 202); 
      expect(res.payload).to.be.a('string'); 
      expect(res.payload).to.have.length(16); 
      done(); 
      }); 
     }); 
     }); 
    }); 

回答

3

這是因爲它是摩卡工作的方式。異步調用中的異常需要被捕獲並傳遞給完成的回調,這甚至包括AssertionErrors。摩卡文檔有錯誤,我打開了一個GitHub問題來解決這個問題(https://github.com/visionmedia/mocha/issues/982)。