2013-04-20 22 views
2

當我所有的測試通過時,一切都按預期工作。然而,當測試失敗,任何記者活躍,亞軍打印代表STRACK跟蹤,而不是漂亮的格式錯誤消息我在屏幕截圖當摩卡測試失敗時,它將一個對象記錄到控制檯

> mocha --reporter spec --ui bdd --ignore-leaks 
Site Routes 

GET /sites 
    ✓ should return a 200 OK 
    ✓ should be a JSON response 
    ◦ should be an array: { 
"date": "Fri Apr 19 2013 16:09:20 GMT-0400 (EDT)", 
"process": { 
    "pid": 34270, 
    "uid": 1032026306, 
    "gid": 116266774, 
    "cwd": "/Users/squinn/Projects/armada", 
    "execPath": "/usr/local/Cellar/node/0.10.4/bin/node", 
    "version": "v0.10.4", 
    "argv": [ 
    "node", 
    "/Users/squinn/Projects/armada/node_modules/mocha/bin/_mocha", 
    "--reporter", 
    "spec", 
    "--ui", 
    "bdd", 
    "--ignore-leaks" 
    ], 
    "memoryUsage": { 
    "rss": 55717888, 
    "heapTotal": 51000320, 
    "heapUsed": 22281672 
    } 
}, 
"os": { 
    "loadavg": [ 
    1.7001953125, 
    1.66162109375, 
    1.58984375 
    ], 
    "uptime": 374244 
}, 
"trace": [ 
    { 
    "column": 10, 
    "file": "/Users/squinn/Projects/armada/node_modules/should/lib/should.js", 
    "function": "Object.Assertion", 
    "line": 368, 
    "method": "Assertion", 
    "native": false 
    }, 
    { 
    "column": 43, 
    "file": "/Users/squinn/Projects/armada/test/route.sites.js", 
    "function": "null.callback", 
    "line": 47, 
    "method": "callback", 
    "native": false 
    }, 
    { 
    "column": 21, 
    "file": "/Users/squinn/Projects/armada/node_modules/request/main.js", 
    "function": "", 
    "line": 290, 
    "method": null, 
    "native": false 
    }, 
    { 
    "column": 17, 
    "file": "events.js", 
    "function": "EventEmitter.emit", 
    "line": 95, 
    "method": "emit", 
    "native": false 
    }, 
    { 
    "column": 54, 
    "file": "/Users/squinn/Projects/armada/node_modules/request/main.js", 
    "function": "", 
    "line": 278, 
    "method": null, 
    "native": false 
    }, 
    { 
    "column": 20, 
    "file": "events.js", 
    "function": "IncomingMessage.EventEmitter.emit", 
    "line": 117, 
    "method": "EventEmitter.emit", 
    "native": false 
    }, 
    { 
    "column": 14, 
    "file": "_stream_readable.js", 
    "function": null, 
    "line": 883, 
    "method": null, 
    "native": false 
    }, 
    { 
    "column": 13, 
    "file": "node.js", 
    "function": "process._tickCallback", 
    "line": 415, 
    "method": "_tickCallback", 
    "native": false 
    } 
], 
"stack": [ 
    "AssertionError: expected [ { user: 'test',", 
    " group: 'group',", 
    " name: 'post-test',", 
    " domain: 'localhost:9000',", 
    " _id: '51718b214805d8fe83000003',", 
    " __v: 0,", 
    " modules: [],", 
    " labels: [ 'Test' ] } ] not to be an instance of Array: expected [ { user: 'test',", 
    " group: 'group',", 
    " name: 'post-test',", 
    " domain: 'localhost:9000',", 
    " _id: '51718b214805d8fe83000003',", 
    " __v: 0,", 
    " modules: [],", 
    " labels: [ 'Test' ] } ] not to be an instance of Array", 
    " at Object.Assertion (/Users/squinn/Projects/armada/node_modules/should/lib/should.js:368:10)", 
    " at null.callback (/Users/squinn/Projects/armada/test/route.sites.js:47:43)", 
    " at null.<anonymous> (/Users/squinn/Projects/armada/node_modules/request/main.js:290:21)", 
    " at EventEmitter.emit (events.js:95:17)", 
    " at IncomingMessage.<anonymous> (/Users/squinn/Projects/armada/node_modules/request/main.js:278:54)", 
    " at IncomingMessage.EventEmitter.emit (events.js:117:20)", 
    " at _stream_readable.js:883:14", 
    " at process._tickCallback (node.js:415:13)" 
], 
"level": "error", 
"message": "uncaughtException" 
} 
npm ERR! Test failed. See above for more details. 
npm ERR! not ok code 0 

這裏看到一個物體是測試

describe('GET /sites', function(){ 

    var url = 'http://localhost:' + app.get('api').port + '/sites'; 

    it('should return a 200 OK', function (done){ 
    request(url, function (err, res){ 
     res.should.have.status(200); 
     done(); 
    }); 
    }); 

    it('should be a JSON response', function (done){ 
    request(url, function (err, res, body){ 
     res.should.be.json; 
     done(); 
    }) 
    }); 

    it('should be an array', function (done){ 
    request(url, function (err, res, body){ 
     JSON.parse(body).should.not.be.an.instanceOf(Array); 
     done(); 
    }); 
    }); 

}); 

任何線索,爲什麼我沒有得到美麗的景色?

回答

0

您使用--ignore-leaks表明您使用的是過時的摩卡版本。 (因爲變更爲--check-leaks。)

安裝最新版本npm install -g mocha

此外,你似乎得到了JSON記者。檢查你是否有test/mocha.opts文件,但我不明白爲什麼它不會被你的命令行參數覆蓋。

+0

感謝您的回答,但不幸的是不是解決方案。沒有摩卡選擇,因爲我更喜歡在make文件中使用內聯命令。 – srquinn 2013-04-21 12:05:44

1

由於異步行爲,對象被記錄到控制檯。 should.js正確拋出錯誤,但不作爲done()回調的參數。我通過在try/catch語句中包裝來修復,所以我可以將錯誤傳遞給done()

it('should return a 200 OK', function (done){ 
    request(url, function (err, res){ 
    try { 
     res.should.have.status(200); 
    } 
    catch (e){ 
     done(e); 
    } 
    }); 
});