[英]Angular 1.1.5 test promise-based service
We use karma to unit test our angular services, these services contains $http calls, so we have a mocked $httpbackend in place so we can run the app without server and db. 我们使用karma对我们的角度服务进行单元测试,这些服务包含$ http调用,因此我们有一个模拟的$ httpbackend,所以我们可以在没有server和db的情况下运行应用程序。 this works fine, a service can call $http("someurl?id=1234") and we get the right data back. 这工作正常,服务可以调用$ http(“someurl?id = 1234”),我们得到正确的数据。
But when we try to do the same thing in unit tests, we can't get it to work, the promise never resolves, when it involves $http 但是当我们尝试在单元测试中做同样的事情时,我们无法让它工作,承诺永远不会解决,当它涉及$ http
The service: 服务:
getAllowedTypes: function (contentId) {
var deferred = $q.defer();
$http.get(getChildContentTypesUrl(contentId))
.success(function (data, status, headers, config) {
deferred.resolve(data);
}).
error(function (data, status, headers, config) {
deferred.reject('Failed to retreive data for content id ' + contentId);
});
return deferred.promise;
}
The mocked $httpbackend 模拟的$ httpbackend
$httpBackend
.whenGET(mocksUtills.urlRegex('/someurl'))
.respond(returnAllowedChildren); //returns a json object and httpstatus:200
The test 考试
it('should return a allowed content type collection given a document id', function(){
var collection;
contentTypeResource.getAllowedTypes(1234).then(function(result){
collection = result;
});
$rootScope.$digest();
expect(collection.length).toBe(3);
});
but collection is undefined, .then() is never called. 但是收集是未定义的,.then()永远不会被调用。
tried pretty much everything to get the promise to resolve, $rootScope.$apply(), $digest, $httpBacke.flush(), but nothing works 尝试了很多东西以获得解决的承诺,$ rootScope。$ apply(),$ digest,$ httpBacke.flush(),但没有任何作品
So mocked $httpBackend works when called from controllers in app, but not when services is called directly in karma unit tests 因此,当应用程序中的控制器调用时,模拟的$ httpBackend可以正常工作,但是当在业力单元测试中直接调用服务时则不行
You should not need to digest twice, since $httpBackend.flush() calls digest itself. 你不应该需要消化两次,因为$ httpBackend.flush()会调用digest本身。 You have to make the call, call digest to resolve the request interceptors, the call flush. 你必须拨打电话,调用摘要来解析请求拦截器,调用flush。
Here is a working Plnkr: http://plnkr.co/edit/FiYY1jT6dYrDhroRpFG1?p=preview 这是一个有效的Plnkr: http ://plnkr.co/edit/FiYY1jT6dYrDhroRpFG1?p = preview
In your case, you have to $digest twice, once for $httpBackend, and again for your own deferred. 在你的情况下,你必须$ digest两次,一次是$ httpBackend,再次是你自己的延迟。
So: 所以:
it('should return a allowed content type collection given a document id', function(){
var collection;
contentTypeResource.getAllowedTypes(1234).then(function(result){
collection = result;
});
$httpBackend.flush();
$rootScope.$digest();
expect(collection.length).toBe(3);
});
You're almost there. 你快到了。 In your case, you just need to force a digest cycle before flushing the HTTP backend. 在您的情况下,您只需要在刷新HTTP后端之前强制执行摘要循环。 See sample code below. 请参阅下面的示例代码
it('should return a allowed content type collection given a document id', function(){
var collection;
contentTypeResource.getAllowedTypes(1234).then(function(result){
collection = result;
});
$rootScope.$digest();
$httpBackend.flush();
expect(collection.length).toBe(3);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.