【发布时间】:2016-08-08 09:10:38
【问题描述】:
在 Angular 1 中,如果我写了一个组件,我会确保:
- 依赖项,例如服务被模拟。
- 为组件中的方法编写单元测试。
-
组件正在调用服务来检索数据
expect(service.method).toHaveBeenCalled() - 组件正在更新给定更新模型的视图。
我一直在对Angular 2组件测试进行一些研究,对于我能找到的所有文章,似乎都在进行如下测试;
- 创建一个返回固定结果的服务模拟(例如:'Test Quote')
- 期望视图包含来自模拟服务的结果(例如,某处有一个具有
<div>Test Quote</div>的 div)。
以下是此类文章的一些示例(基本上是 google 上“Angular 2 组件测试”的顶级结果)
- http://chariotsolutions.com/blog/post/testing-angular-2-components-unit-tests-testcomponentbuilder/
- http://blog.rangle.io/testing-angular-2-applications/
- http://www.itsmycodeblog.com/angular2-unit-testing-a-component/
- https://developers.livechatinc.com/blog/testing-angular-2-apps-dependency-injection-and-components/
由于 NG2 在其测试框架@angular/core/testing 中没有提供任何间谍,建议完全避免使用粗体的步骤?还是我们应该包括茉莉花来接触间谍?
【问题讨论】:
标签: javascript unit-testing testing angular