本文介紹了如何在 Angular 模塊運行塊中測試 Jasmine 代碼的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我想 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 代碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!