Closed RammusXu closed 8 years ago
.catch(function(err) {
console.log("err", err);
should.not.exist(err);
// should.not.exist(err);
done();
});
上面這裡把這一行拿掉試試看?
should.not.exist(err);
因為會進來這裡一定有 err,或者可以改成 should.exist(err);
試試看
@dca
我是希望console能夠顯示錯誤是什麼,所以才故意放should.not.exist(err);
應該是要顯示
CompanyProfile
- try it
1) duplate username will not save anything
- shouldn't save same profile.username
0 passing (65ms)
2 pending
1 failing
1) CompanyProfile duplate username will not save anything:
AssertionError: expected 'err' to not exist
at doAsserterAsyncAndAddThen (/Users/rammus/git_workspace/Ping/app/node_modules/chai-as-promised/lib/chai-as-promised.js:296:33)
at null.<anonymous> (/Users/rammus/git_workspace/Ping/app/node_modules/chai-as-promised/lib/chai-as-promised.js:286:25)
at Object.defineProperty.get (/Users/rammus/git_workspace/Ping/app/node_modules/chai/lib/chai/utils/overwriteProperty.js:50:37)
at Object.should.not.exist (/Users/rammus/git_workspace/Ping/app/node_modules/chai/lib/chai/interface/should.js:190:37)
at Context.<anonymous> (profile_test.js:77:20)
at clearDB (profile_test.js:31:16)
at profile_test.js:37:20
at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:284:19
at open (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:511:17)
at NativeConnection.Connection.onOpen (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:521:5)
at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:477:11
at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:60:5
at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/lib/db.js:234:5
at connectHandler (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/lib/server.js:306:7)
at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:540:23
at commandCallback (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1161:9)
at Callbacks.emit (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3)
at null.messageHandler (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:295:23)
at Socket.<anonymous> (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:285:22)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:529:20)
而不是Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
感謝FB:Chiahao Lin 大大的解法 拿掉callback: done 直接return promise就可以了 REF: http://tobyho.com/2015/12/16/mocha-with-promises/
describe('CompanyProfile', function() {
it('duplate username will not save anything', function() {
return profileUtil.createUser('test', 'qwer1234', 'test', 'Company')
.then(function(value, some) {
console.log("value", value);
// should.not.exist(value);
return profileUtil.createUser('test', 'qwer1234', 'test2', 'Company');
})
.then(function(value) {
console.log("in2");
})
.catch(function(err) {
console.log("err", err);
should.not.exist(err);
// should.not.exist(err);
});
});
硬要用 done() 的話,可能是 error 觸發的時間比 mocha 設定 timeout 的時間還久。 所以可以拉長 timeout 時間看看。
describe('CompanyProfile', function() {
this.timeout(20000) // 拉長點,這個地方有個限制就是 describe 的 callback 不可以用 es6 的 arrow function 撰寫
it('duplate username will not save anything', function() {
return profileUtil.createUser('test', 'qwer1234', 'test', 'Company')
.then(function(value, some) {
console.log("value", value);
// should.not.exist(value);
return profileUtil.createUser('test', 'qwer1234', 'test2', 'Company');
})
.then(function(value) {
console.log("in2");
})
.catch(function(err) {
console.log("err", err);
should.not.exist(err);
// should.not.exist(err);
done();
});
});
感謝 @purepennons @dca 大大的幫忙 最後做出來的成果是
it('duplate username will not save anything', function() {
return profileUtil.createUser('test', 'qwer1234', 'test', 'Company')
.then(function(value, some) {
should.exist(value);
return profileUtil.createUser('test', 'qwer1234', 'test2', 'Company');
})
.then(function(value) {
should.not.exist(value);
}, function(err) {
(err.message).should.include('E11000 duplicate key error collection: test.users index: system_parameter_1_email_1 dup');
})
.catch(function(err) {
console.log("err", err);
should.not.exist(err);
});
});
目的
測試Promise.catch的值是否正確
操作流程
mocha test.js
遇到的問題
在catch裡面放assert會造成mocha 無法執行到done() 所以會timeout
嘗試過的解法
各種google只找到怎麼測試Promise.resolve,但是都沒有提到catch裡面放assert
程式碼
下面附上我的程式碼以及我遇到的錯誤
程式碼
錯誤