AngularJs Jasmine单元测试中的$httpBackend
$httpBackend in AngularJs Jasmine unit test
我无法使单元测试正常工作。我有一个$scope数组,它一开始是空的,但应该用$http.get()填充。在实际环境中,数组中大约有15个对象,但对于我的单元测试,我只获得了2个。对于单元测试,我有:
expect($scope.stuff.length).toBe(2);
但是jasmine的错误是:0应该是2。
这是我的控制器js:
$scope.stuff = [];
$scope.getStuff = function () {
var url = site.root + 'api/stuff';
$http.get(url)
.success(function (data) {
$scope.stuff = data;
})
.error(function(error) {
console.log(error);
});
};
我的控制器.spec.js是:
/// <reference path="../../../scripts/angular-loader.min.js" />
/// <reference path="../../../scripts/angular.min.js" />
/// <reference path="../../../scripts/angular-mocks.js" />
/// <reference path="../../../scripts/angular-resource.js" />
/// <reference path="../../../scripts/controller.js" />
beforeEach(module('ui.router'));
beforeEach(module('ngResource'));
beforeEach(module('ngMockE2E'));
beforeEach(module('myApplication'));
var $scope;
var controller;
var httpLocalBackend;
beforeEach(inject(function ($rootScope, $controller, $injector) {
$scope = $rootScope.$new();
controller = $controller("StuffController", {
$scope: $scope
});
}));
beforeEach(inject(function ($httpBackend) {
httpLocalBackend = $httpBackend;
}));
it('should get stuff', function () {
var url = '/api/stuff';
var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
httpLocalBackend.expectGET(url).respond(200, httpResponse);
$scope.getStuff();
expect($scope.stuff.length).toBe(2);
//httpLocalBackend.flush();
} );
现在,很明显,我更改了变量名等等,因为这是为了工作,但希望这是足够的信息,任何人都可以帮助我。如果需要,我可以提供更多。在取消对.flush()行的注释时,我也会遇到第二个错误,但稍后我会处理这个问题。
任何帮助都将不胜感激,并提前表示感谢!
编辑:
终于成功了!这是我的最后一个代码:
it('should get stuff', function () {
var url = '/api/stuff';
var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
httpLocalBackend.expectGET(url).respond(200, httpResponse);
$scope.getStuff();
httpLocalBackend.flush();
expect($scope.stuff.length).toBe(2);
} );
编辑2:我遇到了另一个问题,我认为这可能是造成这种情况的根本原因。请参阅在启动时调用函数时单元测试失败
您需要将httpLocalBackend.flush()语句放在expect($scope.stuff.length).toBe(2)之前。发出请求后,您需要刷新它,以便在客户端代码中提供数据。
it('should get stuff', function () {
var url = '/api/roles';
var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
scope.getStuff(); //Just moved this from after expectGET
httpLocalBackend.expectGET(url).respond(200, httpResponse);
httpLocalBackend.flush();
expect($scope.stuff.length).toBe(2);
} );
试试这个。
相关文章:
- 使用jasmine模拟对服务器的调用
- 为什么jasmine期望不验证是否抛出了错误
- 模拟chrome.storage.local函数使用Jasmine
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- 为什么spyOn"停止函数“”的所有执行;Jasmine(要求澄清Jasmine 2.2间谍文件)
- 可以't在Jasmine中调用Javascript函数
- Jasmine单元测试在监视服务方法时失败
- 如何使用Jasmine测试Knockout.js点击绑定
- 如何在jasmine测试中为有角度的项目注入依赖项
- 仅首先需要使用jasmine从节点添加一个文件
- 使用Jasmine捕获传递给不同JavaScript文件中函数的参数
- 什么'与Jasmine相比,使用Sinon.js的优势是什么;内置于间谍
- 测试iFrame的位置.href是在jasmine单元测试中设置的
- 在Karma Jasmine单元测试用例中检查日期格式
- httpBackend模拟json文件响应
- 使用 $httpBackend、Jasmine 测试$http请求中特定标头的角度项目服务
- 使用Jasmine's exceptGET/$httpBackend,what'respond()的好处
- 使用$httpBackend测试Jasmine/Karma的AngularJS不起作用
- AngularJs Jasmine单元测试中的$httpBackend
- AngularJS&Karma Jasmine-仅当存在一个或多个挂起的请求时才刷新$httpbackend