我有以下摩卡测试。我试图提出断言并测试失败。每当回调中的断言失败时,mocha仍然认为测试通过了。我已经尝试了assert和expect模块。
var sendSecureQueue = SecureQueue(secureBullOptions.encryption, "Server A");
var receiveSecureQueue = SecureQueue(secureBullOptions.encryption, "Server A");
describe('SecureQueue', function () {
describe('and() and process() - unencrypted', function () {
it('should be equal to each other, for both Queue and SecureQueue', function () {
var sendSecureData = {msg: "Hello"};
console.log("Send Secure Data: " + util.inspect(sendSecureData));
sendSecureQueue.add(sendSecureData);
receiveSecureQueue.process(function (job, done) {
var receivedSecureData = job.data;
console.log("Received secure data", util.inspect(receivedSecureData));
//**Testing Force a Failure**
//assert.equal(1,2);
//**Testing Force a Failure**
expect(1).to.be.equal(2);
done();
});
});
});
});
Mocha输出 - 带断言/期望:
SecureQueue
and() and process() - unencrypted
Send Secure Data: { msg: 'Hello' }
✓ should be equal to each other, for both Queue and SecureQueue
Received secure data { msg: 'Hello' }
(node:16349) Warning: a promise was created in a handler at /usr/apps/myapp/node_modules/bull/lib/queue.js:688:9 but was not returned from it, see ...
at new Promise (/usr/apps/myapp/node_modules/bluebird/js/release/promise.js:77:14)
1 passing (120ms)
(node:16349) Warning: .then() only accepts functions but was passed: [object Object]
Mocha输出 - 评论断言/期望:
SecureQueue
and() and process() - unencrypted
Send Secure Data: { msg: 'Hello' }
✓ should be equal to each other, for both Queue and SecureQueue
1 passing (102ms)
Received secure data { msg: 'Hello' }
答案 0 :(得分:2)
由于您测试异步行为(在这种情况下是回调而不是承诺),您需要向it()
(通常称为done
)添加回调以告诉mocha您的测试已完成:
it('should be equal to each other, for both Queue and SecureQueue', function (done) {
var sendSecureData = {msg: "Hello"};
console.log("Send Secure Data: " + util.inspect(sendSecureData));
sendSecureQueue.add(sendSecureData);
receiveSecureQueue.process(function (job) {
var receivedSecureData = job.data;
console.log("Received secure data", util.inspect(receivedSecureData));
expect(1).to.equal(2);
done();
});
});
expect的语法不应同时包含be
和equal
,可以使用expect.js
在这两种表单中的任何一种中写入相等性测试:
expect(1).to.equal(2);
expect(1).to.be(2);