angularjs - 将失败消息分配给期望调用

标签 angularjs mocha.js chai

我目前正在学习 AngularJS,其中一部分涉及创建测试。目前,我正在尝试研究如何为失败的测试创建更有用的错误消息。例如,如果我在 Java 领域并编写 JUnit 测试,我会做类似这样的事情:

assertTrue( "The foo value should be true at this point", bar.isFoo() );

这样,如果检查失败,我将在日志中获取第一个参数。

对于 mocha 中的 bool 检查(使用 chai 和 sinon,以防有所不同),我有...
expect(broadcastSpy.calledWith('order.add')).to.be.true;

如果失败,那么我得到以下信息:
expected false to be true
AssertionError: expected false to be true

有没有办法在测试我的应用程序时复制有用的失败消息?

最佳答案

我在 answer 中提到了一些关于此的内容。另一个问题。我刚刚检查了 Chai 的代码,以确保自从我写下该答案并发现以下内容仍然正确后没有任何变化。编码:

expect(foo).to.be.true;

不接受自定义消息,因为 true是通过 getter 获取其值的属性.但是,您可以这样做:
expect(foo).to.equal(true, "foo should be true");

如果断言失败,则获取自定义消息。

Chai 的断言接口(interface)支持所有断言的消息,例如:
assert.isTrue(foo, "foo should be true");

关于angularjs - 将失败消息分配给期望调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23762633/

相关文章:

javascript - 模板未在 Angular 1.5 指令中加载

node.js - Supertest无法测试重复的post请求

angularjs - 如何在 Sinon JS 的单元测试中处理 sinon.stub().throws()

arrays - 错误: [$injector:unpr] Unknown provider: $stateProvider <- $state

angularjs - chai-as-promised 测试不适用于 $q promise

angularjs - Firebase auth.$createUserWithEmailAndPassword 不起作用

javascript - 简单的 Angular JS 应用程序无法正常工作且未显示任何错误

javascript - 在 AngularJS Material 中使用 md-virtual-repeat 进行无限滚动

node.js - 如何对池连接进行单元测试?

javascript - 要求在测试文件中导出默认模块