[英]AngularJs Jasmine Unit test fails with Unexpected Request
Following is my myService.spec.js : 以下是我的myService.spec.js:
'use strict';
describe('myService', function () {
var dependentService,dependentService1,rootScope,$q;
beforeEach(module('myModule.myConfig'));
beforeEach(module('myModule'));
beforeEach(inject(function (_myService_, _$rootScope_,
_$q_,_dependentService1_,_dependentService_) {
myService= _myService_;
rootScope = _$rootScope_.$new();
$q = _$q_;
dependentService1= _dependentService1_;
dependentService= _dependentService_;
spyOn(dependentService1,'setPath');
spyOn(dependentService,'get');
spyOn($q,'all').and.callFake(function(){
var deferred = _$q_.defer();
if($q.all.calls.count() === 1){
deferred.resolve([{path:'./abcd'}]);
}else if($q.all.calls.count() === 2){
deferred.resolve([{path:'./abcd','user': {'userId':'xyz',
'groups':['G1']}}]);
}else{
deferred.resolve({});
}
return deferred.promise;
});
}));
it('should load path, information',function(){
var promise = myService.load();
rootScope.$apply();
expect(dependentService.get).toHaveBeenCalled();
expect(dependentService1.setPath).toHaveBeenCalledWith('./abcd');
});
});
And here is my MyService.js 这是我的MyService.js
'use strict';
function myService($q,dependentService1,dependentService){
var appConfigLoaded = false;
function _loadPath(){
return dependentService.get(dependentService1.url);
}
return {
load : function(){
var loadPath = _loadPath(),
finalDeferred = $q.defer();
$q.all([loadPath ]).then(function (results) {
var path = results[0].path ,
user = results[0].user;
dependentService1.setPath(path);
$q.all([_loadDataPromise1(),_loadDataPromise2()]).then(function(results){
finalDeferred.resolve(results);
},function(reason){
finalDeferred.reject(reason);
});
},function(reason){
finalDeferred.reject(reason);
});
return finalDeferred.promise;
}
};
}
angular.module('myModule.myConfig').service('myService', MyService);
Following functions and service which holds them are omitted for brevity but they return promise. 为简洁起见,省略了保留它们的以下功能和服务,但它们返回了承诺。 I have spied them as well just like I have spied other two services. 我已经监视了它们,就像我监视了其他两项服务一样。
loadDataPromise1() and loadDataPromise1()
Now I get some error like Unexpected request GET with URL which points to some headers.template.html. 现在,我得到一些错误,例如带有URL的意外请求GET,它指向一些headers.template.html。 But I am not even making any call to http to get such template nor any of the functions do. 但是我什至没有打电话给http来获得这样的模板,也没有任何功能可以做到。 which ever call $http.get I have spied them. 曾经叫$ http.get我监视了他们。
I tried with 我尝试过
$httpBackend.flush();
but the same error occur. 但是会发生相同的错误。 May be I am doing something basic thing in a wrong way. 可能是我在以错误的方式做一些基本的事情。
If I remove $rootScope.apply()
then error goes away. 如果删除$rootScope.apply()
错误就会消失。 However .then() function in my service attached to first call to $q.all()
is not called. 但是,我的服务中附加到第一次调用$q.all()
函数没有被调用。
Any pointers to help me out? 有什么指针可以帮助我吗?
Do you have a default route with a templateURL in your application routing? 您的应用程序路由中是否有带有templateURL的默认路由? You are probably running into this issue: 您可能会遇到此问题:
https://github.com/angular/angular.js/issues/2717 https://github.com/angular/angular.js/issues/2717
The workaround (which is annoying) is to put an expectGET
for the templateURL in your beforeEach
and then flush it. 解决方法(这很烦人)是将expectGET
中的templateURL的beforeEach
然后刷新。
$httpBackend.expectGET('path/to/template/defaulttemplate.html').respond(200, '');
$httpBackend.flush();
You can do it anywhere in there - I try and keep it at the top or bottom so it is obvious that is a workaround and not part of the testing code. 您可以在任何地方进行操作-我尝试将其保持在顶部或底部,因此很明显这是一种解决方法,而不是测试代码的一部分。 Note you will have to put those lines in EVERY test file since the routing is part of app (not the module under test). 请注意,由于路由是应用程序(而不是被测模块)的一部分,因此您必须将这些行放在每个测试文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.