我需要对我得到的 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.
最佳答案
当您使用异步代码(和 promise )时,就会发生这种情况。从您的测试代码来看,方法 sslCertificate.get() 返回一个 Promise。该 promise 将异步得到解决(成功执行)或拒绝(抛出错误)。在 JS 中,异步执行只有在当前同步执行暂停/完成后才开始。
在测试上下文中,您可以在 Promise 解析时传递回调方法(使用 .then())。此回调仅在 promise 解决后执行,并且由于您的测试代码不会为此执行而暂停,因此它会成功完成 - 这意味着 sslCertificate.get('instagram.com').then(callback) 永远不会引发任何错误或异常。执行测试后,promise 有机会解析,现在异步执行您的回调。因此,您会收到 UnhandledPromiseRejectionWarning: AssertionError。
这可以通过使用 mocha 异步测试以两种方式来处理:
方法 1:使用 async/await(我个人推荐的可读性):
- 使您的测试函数异步。
- 等待 Promise 解决。
- 执行你的断言。
这是一些代码:
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 的完成回调 - 不是我最喜欢的用例 - 当有回调但不涉及 promise 时使用
- 为测试函数添加一个名为“done”的参数(该名称实际上是任意的)。
- 在传递给“then()”的回调函数中执行所有必需的断言。
- 最后调用done()。
添加“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.
});
});
来自官方文档的更多详细信息:https://mochajs.org/#asynchronous-code
关于node.js - 即使断言失败,Mocha 测试也会通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63866465/