2012-09-30 198 views
0

爲什麼使用摩卡進行測試時以下代碼不通過?用摩卡測試json.parse

我的例子模塊:

module.exports = function(){ 
    JSON.parse("this is not json") 
} 

和我test.js:

var should = require("should") 
var module = require("./module") 

describe("error testing", function(){ 
    it("should throw an error", function(done){ 
    module().should.throw(); 
    done(); 
    }) 
}) 

我希望測試通過,但運行摩卡給了我下面的:

✖ 1 of 1 tests failed: 

1) error testing should throw an error: 
    SyntaxError: Unexpected token h 
    at Object.parse (native) 
    at module.js:9:8 
    at Context.<anonymous> (test.js:6:5) 
    at Test.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:145:15) 
    at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:270:10) 
    at /usr/local/lib/node_modules/mocha/lib/runner.js:314:12 
    at next (/usr/local/lib/node_modules/mocha/lib/runner.js:198:14) 
    at /usr/local/lib/node_modules/mocha/lib/runner.js:207:7 
    at next (/usr/local/lib/node_modules/mocha/lib/runner.js:157:23) 
    at Array.0 (/usr/local/lib/node_modules/mocha/lib/runner.js:175:5) 
    at EventEmitter._tickCallback (node.js:192:40) 

回答

1

應.throw()期望在函數上調用,而不是函數的結果。更改此:

module().should.throw(); 

這樣:

module.should.throw();