在Jasmine中使用循环(带注入服务) [英] Using loops in Jasmine (with injected service)
问题描述
我正在使用 jasmine 2.3。
如下: http://tosbourn.com/using-loops-in-jasmine/ 我将FOR循环直接放在' describe
'嵌套函数
I'm using jasmine 2.3.
Following this: http://tosbourn.com/using-loops-in-jasmine/ I placed a FOR loop directly in the 'describe
' nested function
describe('service Profile', function() {
var ProfileService;
beforeEach(function() {
module('app.services.profile');
inject(function(_ProfileService_) {
ProfileService = _ProfileService_;
});
});
describe('method setProfile', function() {
function setProfileTest(key) {
it('should set the object profile', function() {
expect(ProfileService.profile[key]).toBeUndefined();
});
}
for (var key in ProfileService.profile) {
if (ProfileService.profile.hasOwnProperty(key)) {
setProfileTest(key);
}
}
});
});
问题是在'之外
'function, ProfileService
仍然未定义
。
The problem is that outside the 'it
' function, ProfileService
still undefined
.
推荐答案
由于您需要注入 ProfileService
,因此循环必须在 beforeEach
块之后运行。
Since you need ProfileService
to be injected, the loop must run after the beforeEach
block.
我可以看到两种解决方案。要么:
I can see two solutions. Either:
使用硬编码的配置文件列表并迭代它们。例如 - 而不是
Use a hard-coded list of profiles and iterate through them. Eg- instead of
for (var key in ProfileService.profile) {
do
for (var key in ['profile1', 'profile2'...) {
或从外部文件加载配置文件。
or load the profiles from an external file.
OR
将for循环放在中
测试:
Put the for loop inside of the it
test:
it('should set the object profile', function() {
for (var key in ProfileService.profile) {
if (ProfileService.profile.hasOwnProperty(key)) {
expect(ProfileService.profile[key]).toBeUndefined();
}
}
});
这篇关于在Jasmine中使用循环(带注入服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!