问题
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