javascript - Mocha 回调中的断言不起作用

标签 javascript node.js mocha.js

我有以下 Mocha 测试。我正在尝试提出断言并测试失败。每当回调中断言失败时,mocha 仍然认为测试通过。我已经尝试了断言和期望模块。

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' }

最佳答案

由于您正在测试异步行为(在本例中是回调而不是 promise ),因此您需要将回调添加到 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 的语法不应同时拥有 beequal ,可以使用 expect.js 以这两种形式之一编写相等性测试:

expect(1).to.equal(2);
expect(1).to.be(2);

关于javascript - Mocha 回调中的断言不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41711500/

相关文章:

javascript - Dashboard Widget 首选项实际保存在哪里?

javascript - npm 错误!失败...开发脚本 'webpack-dev-server'

javascript - 让 React 路由器与 Spring 端点一起工作

javascript - Maven 的 Mocha 插件

coffeescript - 我怎样才能与 super 测试同步测试

javascript - 尝试修复嵌套表单

javascript - 我应该在 ExtJS 4 MVC 中的哪里定义全局函数?

javascript - WebGL 从缓冲区中删除对象

node.js - 使用 Google API 在工作表中设置数据时,Google 表格会自动更改日期格式

javascript - 通过异步 JavaScript (Mocha) 进行循环测试