即使断言失败,摩卡测试也会通过

Mocha test passing even when assertion is failing

我需要 运行 对我得到的 JS 对象进行断言。这里的问题是,即使我的断言失败,测试仍然显示为通过;我该如何解决?

代码:

  var expect = require('chai').expect
    const sslCertificate = require('get-ssl-certificate')

    describe('ssl certificate verification',()=>{
    it('verifies the issuer of the certificate',()=>{
        sslCertificate.get('instagram.com').then(function (certificate) {
           console.log(typeof certificate.issuer)
           console.log(certificate.issuer.O)
           console.log(certificate.issuer.CN)
           console.log(certificate.subject.CN)

           expect(certificate.issuer).to.include({CN: 'DigiCert SHA2 High Assurance Server CA'});
           expect(certificate.issuer).which.is.an('object').to.haveOwnProperty('CN')
        })
    })
})

终端命令:

mocha myFile.js

输出

ssl certificate verification
    √ verifies the issue of the certificate


  1 passing (46ms)

object
DigiCert Inc
DigiCert SHA2 High Assurance Server CA
*.instagram.com

断言失败,但通过测试输出

 expect(certificate.issuer).to.include({CN: 'a'});



    ssl certificate verification
    √ verifies the issue of the certificate


  1 passing (43ms)

object
DigiCert Inc
DigiCert SHA2 High Assurance Server CA
*.instagram.com
(node:13712) UnhandledPromiseRejectionWarning: AssertionError: expected { Object (C, O, ...) } to have property 'CN' of 'a', but got 'DigiCert SHA2 High Assurance Server CA'
    at D:\a10\cypress\integration\ssli5_2v4\bypassFlow\new.js:15:42
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:13712) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13712) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

当您使用异步代码(和承诺)时会发生这种情况。从您的测试代码看来,方法 sslCertificate.get() returns 是一个 Promise。这个承诺将被异步解决(成功执行)或拒绝(抛出错误)。在 JS 中,异步执行仅在当前同步执行 pauses/completes.

之后才开始

在您的测试上下文中,您在 promise 解析时传递了一个回调方法(使用 .then())。只有在 promise 得到解决后才会执行此回调,并且由于您的测试代码不会为此执行而暂停,因此它会成功完成 - 这意味着 sslCertificate.get('instagram.com').then(callback) 永远不会抛出任何错误或异常.测试执行后,promise 有机会解析并异步执行您的回调。因此,您会收到 UnhandledPromiseRejectionWarning: AssertionError.

这可以通过两种方式使用 mocha 异步测试来处理:

方法一:使用async/await(我个人的可读性推荐):

  1. 使您的测试函数异步。
  2. 等待承诺解决。
  3. 执行你的断言。

这是一些代码:

it('verifies the issuer of the certificate', async ()=>{ // This tells the test contains asynchronous code
    const certificate = await sslCertificate.get('instagram.com'); // await gives a chance for the promise to resolve (in which case the certificate will be returned) or reject (in which case an exception will be thrown)

    // You can now perform your assertions on the certificate
    expect(certificate.issuer).to.include({CN: 'DigiCert SHA2 High Assurance Server CA'});
    expect(certificate.issuer).which.is.an('object').to.haveOwnProperty('CN');
});

方法 2:使用来自 mocha 的完成回调 - 不是我最喜欢的用例 - 在有回调但不涉及承诺时使用

  1. 为测试函数添加一个名为'done'的参数(名字真的很随意)
  2. 在传递给 'then()' 的回调函数中执行所有必需的断言。
  3. 最后调用 done()。

添加 'done' 参数会强制 mocha 等待回调 done() 被调用。现在,如果您的断言失败,将永远不会调用 done() 并且测试将因超时错误而失败(当然还有控制台中未处理的拒绝错误)。尽管如此,测试还是会失败。

示例代码:

it('verifies the issuer of the certificate',(done)=>{ // Forces mocha to wait until done() is called
    sslCertificate.get('instagram.com').then(function (certificate) {

       expect(certificate.issuer).to.include({CN: 'DigiCert SHA2 High Assurance Server CA'});
       expect(certificate.issuer).which.is.an('object').to.haveOwnProperty('CN');
       done(); // All assertions done.
    });
});

来自官方文档的更多详细信息:https://mochajs.org/#asynchronous-code