【问题标题】:Mocha test passing even when assertion is failing即使断言失败,摩卡测试也会通过
【发布时间】:2020-12-31 02:56:19
【问题描述】:

我需要在我得到的 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.

【问题讨论】:

    标签: node.js mocha.js assertion


    【解决方案1】:

    当您使用异步代码(和承诺)时,会发生这种情况。从您的测试代码看来,sslCertificate.get() 方法返回了一个 Promise。这个承诺会被异步解决(成功执行)或拒绝(抛出错误)。在 JS 中,异步执行仅在当前同步执行暂停/完成后才开始。

    在您的测试上下文中,当 Promise 解决时,您会传递一个回调方法(使用 .then())。此回调仅在承诺解决后才会执行,并且由于您的测试代码不会为此执行暂停,因此它成功完成 - 这意味着 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 中的 done 回调 - 不是我最喜欢的用例 - 在有回调但不涉及承诺时使用

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多