How to Jasmine test code within angular module run block(如何在 Angular 模块运行块中测试 Jasmine 代码)
本文介绍了如何在 Angular 模块运行块中测试 Jasmine 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想 Jasmine 测试一下是否调用了 Welcome.go.Welcome 是一个 Angular 服务.
I would like to Jasmine test that Welcome.go has been called. Welcome is an angular service.
angular.module('welcome',[])
.run(function(Welcome) {
Welcome.go();
});
这是我目前的测试:
describe('module: welcome', function () {
beforeEach(module('welcome'));
var Welcome;
beforeEach(inject(function(_Welcome_) {
Welcome = _Welcome_;
spyOn(Welcome, 'go');
}));
it('should call Welcome.go', function() {
expect(Welcome.go).toHaveBeenCalled();
});
});
注意:
- welcome(小写w)是模块
- 欢迎(大写 W)是服务
推荐答案
设法弄明白了.这是我想出的:
Managed to figure it out. Here is what I came up with:
'use strict';
describe('module: welcome', function () {
var Welcome;
beforeEach(function() {
module('welcome', function($provide) {
$provide.value('Welcome', {
go: jasmine.createSpy('go')
});
});
inject(function (_Welcome_) {
Welcome = _Welcome_;
})
});
it('should call Welcome.go on module run', function() {
expect(Welcome.go).toHaveBeenCalled();
});
});
这篇关于如何在 Angular 模块运行块中测试 Jasmine 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:如何在 Angular 模块运行块中测试 Jasmine 代码


基础教程推荐
猜你喜欢
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01