Obtaining $httpBackend in QUnit tests

吃可爱长大的小学妹 提交于 2019-12-10 16:47:00

问题


I am writing QUnit tests for an Angular controller. In the setup function of module, I have written the following statements to get an object of $httpBackend:

 var injector = angular.injector(['ng']);
 var httpBackend = injector.get('$httpBackend');

In a test, a mock response for GET is configured as follows:

httpBackend.expectGET(url).respond([]);

The test spec fails at this statement with error: Object doesn't support property or method expectGET

I am able to get other objects like controller, scope injected using the same injector reference.

Did I miss anything here?


回答1:


I asked Scott Allen about this issue and he replied me with a JSFiddle that solves the issue. He stated that the following decorator is essential when we use $httpBackend or any other mock defined in angular-mocks.js in QUnit tests:

$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);

I used this approach in a sample application and blogged my learning: http://sravi-kiran.blogspot.com/2013/06/UnitTestingAngularJsControllerUsingQUnitAndSinon.html




回答2:


You must setup angular to use the $httpBackend from angular-mocks.js Which contains the expectGET method.

But according to the docs it's "Only available with jasmine."



来源:https://stackoverflow.com/questions/16300485/obtaining-httpbackend-in-qunit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!