Sar*_*mad 2 mocha.js node.js assertion
我需要对我得到的 JS 对象运行断言。这里的问题是,即使我的断言失败,测试仍然显示为通过;我该如何修复它?
\n代码:
\n var expect = require('chai').expect\n const sslCertificate = require('get-ssl-certificate')\n\n describe('ssl certificate verification',()=>{\n it('verifies the issuer of the certificate',()=>{\n sslCertificate.get('instagram.com').then(function (certificate) {\n console.log(typeof certificate.issuer)\n console.log(certificate.issuer.O)\n console.log(certificate.issuer.CN)\n console.log(certificate.subject.CN)\n\n expect(certificate.issuer).to.include({CN: 'DigiCert SHA2 High Assurance Server CA'});\n expect(certificate.issuer).which.is.an('object').to.haveOwnProperty('CN')\n })\n })\n})\n
Run Code Online (Sandbox Code Playgroud)\n终端命令:
\nmocha myFile.js\n
Run Code Online (Sandbox Code Playgroud)\n输出
\nssl certificate verification\n \xe2\x88\x9a verifies the issue of the certificate\n\n\n 1 passing (46ms)\n\nobject\nDigiCert Inc\nDigiCert SHA2 High Assurance Server CA\n*.instagram.com\n
Run Code Online (Sandbox Code Playgroud)\n断言失败,但通过测试输出
\n expect(certificate.issuer).to.include({CN: 'a'});\n\n\n\n ssl certificate verification\n \xe2\x88\x9a verifies the issue of the certificate\n\n\n 1 passing (43ms)\n\nobject\nDigiCert Inc\nDigiCert SHA2 High Assurance Server CA\n*.instagram.com\n(node:13712) UnhandledPromiseRejectionWarning: AssertionError: expected { Object (C, O, ...) } to have property 'CN' of 'a', but got 'DigiCert SHA2 High Assurance Server CA'\n at D:\\a10\\cypress\\integration\\ssli5_2v4\\bypassFlow\\new.js:15:42\n at processTicksAndRejections (internal/process/task_queues.js:97:5)\n(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)\n(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.\n
Run Code Online (Sandbox Code Playgroud)\n
当您使用异步代码(和承诺)时,就会发生这种情况。从您的测试代码来看,方法 sslCertificate.get() 返回一个 Promise。该承诺将异步得到解决(成功执行)或拒绝(抛出错误)。在 JS 中,异步执行仅在当前同步执行暂停/完成后才开始。
在测试上下文中,您可以在 Promise 解析时传递回调方法(使用 .then())。此回调仅在承诺解决后执行,并且由于您的测试代码不会为此执行而暂停,因此它会成功完成 - 这意味着 sslCertificate.get('instagram.com').then(callback) 永远不会引发任何错误或异常。执行测试后,promise 有机会解析,现在异步执行您的回调。因此,您会收到 UnhandledPromiseRejectionWarning: AssertionError。
这可以通过使用 mocha 异步测试以两种方式来处理:
方法1:使用async/await(我个人推荐的可读性):
这是一些代码:
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');
});
Run Code Online (Sandbox Code Playgroud)
方法 2:使用 mocha 的完成回调 - 不是我最喜欢的用例 - 当有回调但不涉及承诺时使用
添加 'done' 参数会强制 mocha 等待,直到回调 did() 被调用。现在,如果您的断言失败,则永远不会调用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.
});
});
Run Code Online (Sandbox Code Playgroud)
来自官方文档的更多详细信息:https ://mochajs.org/#asynchronous-code
归档时间: |
|
查看次数: |
1946 次 |
最近记录: |