业力+茉莉花+角+ ui路由器-使用params使用state.go测试解析 [英] Karma + jasmine + angular + ui router - testing for resolve with state.go with params
问题描述
我在我的角度模块"moduleB"中定义了如下状态
I have state defined in my angular module "moduleB" as follows
$stateProvider
.state('stateB', {
parent: 'stateA',
abstract: true,
templateUrl : baseUrl+'/templates/stateB.html'
})
.state('stateB.details', {
url: '/stateB/details/:param1/:param2',
resolve : {
value3 : ['$localStorage', '$stateParams', function($localStorage, $stateParams){
return $localStorage.value3[$stateParams.param1];
}]
},
views : {
'view1' : {
templateUrl : baseUrl+'/templates/view1.html',
controller: 'View1Ctrl'
},
'view2' : {
templateUrl : baseUrl+'/templates/view2.html',
controller : 'View2Ctrl'
}
}
})
我想为解决"编写单元测试,这是我的茉莉花单元测试.
I would like to write unit test for the "resolve" and here is my jasmine unit test.
var rootScope, state, injector, mockLocalStorage, httpBackend;
beforeEach(module('moduleB'));
beforeEach(inject(function($rootScope, $state, $injector, $localStorage, $httpBackend) {
rootScope = $rootScope;
state = $state;
injector = $injector;
httpBackend = $httpBackend;
mockLocalStorage = $localStorage;
}));
it('should should resolve the data', function() {
mockLocalStorage.value3 = {};
mockLocalStorage.value3["1234567890"] = 'resolved-data';
state.go('stateB.details', {
"param1" : "1234567890",
"param2" : true
});
rootScope.$digest();
console.log('state', state);
expect(state.current.name).toBe('stateB.details');
expect(injector.invoke(state.current.resolve.value3)).toBe('resolved-data');
});
1)console.log('state',state)==>打印 '状态',对象{参数:对象{},当前:对象{名称:``,........}
1) console.log('state', state) ==> prints 'state', Object{params: Object{}, current: Object{name: '', ........}
2)Expect(state.current.name).toBe('stateB.details')==>失败并显示错误 预计"为"stateB.details".
2) expect(state.current.name).toBe('stateB.details') ==> fails with error Expected '' to be 'stateB.details'.
3)期望解决==>失败并显示错误 TypeError:未定义"不是对象(正在评估"state.current.resolve.value3")
3) expect for resolve ==> fails with error TypeError: 'undefined' is not an object (evaluating 'state.current.resolve.value3')
有人可以帮忙指出我在想什么吗?
Can anyone help in pointing what am I missing?
更新:
我修改了我的测试,使其在promise的成功块中包含断言. 即使断言为假,它也可以通过测试.
I modified my test to have assertions in the success block of the promise. It passes the test even when the assertions are false.
it('should resolve the data', function() {
mockLocalStorage.value3 = {};
mockLocalStorage.value3["1234567890"] = 'resolved-data';
state.go('stateB.details', {
"param1" : "1234567890",
"param2" : true
}).then(function() {
console.log('state', state);
expect(state.current.name).toBe('stateB.details'); expect(injector.invoke(state.current.resolve.value3)).toBe('resolved-data2');
});;
});
此测试通过,并且"console.log('state',state);"不显示任何内容. 而且,我对"resolved-data2"的断言应该失败,因为期望值是"resolved-data".
This test passes and nothing is displayed for "console.log('state', state);" . And, my assertion for "resolved-data2" should fail as the expected value is "resolved-data".
推荐答案
最后,我解决了这个问题.这是任何有兴趣的人的解决方案.
Finally, I fixed the problem. Here is the solution for any one who is interested in.
1)导入模块
beforeEach(module('stateA'));
beforeEach(module('stateB'));
beforeEach(module('ui.router'));
2)
beforeEach(module(
function($provide) {
$provide.value('$localStorage', mockStorage = { value3: []});
$provide.value('$stateParams', stateParams = { param1: 1234, param2: "true"});
}));
3)单元测试:
it('should go to stateB.details state and resolve the data', function() {
mockStorage.assetInfo[stateParams.value3] = 'resolved-data';
var s = state.get('stateB.details');
expect(injector.invoke(s.resolve.value3)).toEqual('resolved-data');
});
这篇关于业力+茉莉花+角+ ui路由器-使用params使用state.go测试解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!