角 - 茉莉花可以真正XHR执行? [英] Angular - Jasmine can real XHR be executed?
问题描述
我不希望有$ HTTP嘲笑。主要的原因是我写的一些集成测试,所以我想有执行的所有AJAX请求。目前在我的测试中没有被触发。
任何建议多于欢迎。
I do not want to have the $http mocked. Main reason is that I am writing some integration tests so I want to have all AJAX requests executed. Currently in my tests none gets triggered. Any suggestion is more than welcomed.
推荐答案
的角度嘲笑提供嘲笑$ httpBackend当您使用茉莉花测试,但实际$ httpBackend仍然存在。你只需要告诉供应商,当你注入该服务使用原来的。它看起来像这样在您的测试:
The angular mocks provide the mocked $httpBackend when you are using Jasmine tests, but the actual $httpBackend still exists. You just need to tell the provider to use the original when you inject the service. It will look something like this in your test:
var original_http_backend = null;
angular.module('ng').config(['$httpBackendProvider', function($httpBackendProvider) {
original_http_backend = $httpBackendProvider;
}]);
beforeEach(module(function($provide) {
$provide.provider('$httpBackend', original_http_backend)
}));
另外值得指出的是,这种做法,一般,是测试你的前端code一个糟糕的方式。这是因为它增加了一个后端的依赖,所以你不能孤立的前端行为。这是pferred模拟出与响应和测试$ P $。
It's also worth pointing out that this approach, in general, is a BAD way to test your front end code. This is because it adds a backend dependency, so you cannot isolate your front end behavior. It is preferred to mock out the response and test with that.
我也结束了,虽然使用此功能,因为我们的模拟答复后端测试有发展,我不想再重复的对象。这种方法允许我使用他们。
I did end up using this feature though, since our mock responses were developed on the back end for testing there, and I didn't want to repeat the objects. This method allowed me to use them.
这篇关于角 - 茉莉花可以真正XHR执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!