问题描述
我写的 QUnit 为角控制器测试。在模块的设置功能,我写了下面的语句来获取对象的 $ httpBackend
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([]);
测试规范在失败与错误语句:对象不支持属性或方法 expectGET
我能够得到其他对象,如控制器,使用相同的注射器参考范围注入。
I am able to get other objects like controller, scope injected using the same injector reference.
我错过了什么吗?
推荐答案
我问关于这个问题,他回答我一个,解决了问题。他说,下面的装饰是必不可少的,当我们使用$ httpBackend或QUnit的测试中角mocks.js定义的任何其他模拟:
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);
我以前在一个示例应用程序这一做法,并在博客我的学习:的
这篇关于获得$ httpBackend在QUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!