使用$httpBackend测试Jasmine/Karma的AngularJS不起作用
Testing AngularJS with Jasmine / Karma with $httpBackend not working
我正试图通过Karma与Jasmine一起对我的应用程序进行基于Angular的单元测试。该应用程序包括调用GitHub API,提取用户所有转发的名称,并用这些名称填充数组。我正在尝试测试数组是否已填充,但$httpBackend出现了一些问题。
我的控制器的相关部分是:
readmeSearch.controller('ReadMeSearchController', ['RepoSearch', function(RepoSearch) {
var self = this;
self.gitRepoNames = [];
self.doSearch = function() {
var namesPromise =
RepoSearch.query(self.username)
.then(function(repoResponse) {
addRepoNames(repoResponse);
}).catch(function(error) {
console.log('error: ' + error);
});
return namesPromise;
};
addRepoNames = function(response) {
self.repoSearchResult = response.data;
for(var i = 0; i < self.repoSearchResult.length; i++) {
var name = self.repoSearchResult[i]['name']
self.gitRepoNames.push(name);
};
};
}]);
我的RepoSearch工厂是:
readmeSearch.factory('RepoSearch', ['$http', function($http) {
return {
query: function(searchTerm) {
return $http({
url: 'https://api.github.com/users/' + searchTerm + '/repos',
method: 'GET',
params: {
'access_token': gitAccessToken
}
});
}
};
}]);
有问题的测试是:
describe('ReadMeSearchController', function() {
beforeEach(module('ReadMeter'));
var ctrl;
beforeEach(inject(function($controller) {
ctrl = $controller('ReadMeSearchController');
}));
describe('when searching for a user''s repos', function() {
var httpBackend;
beforeEach(inject(function($httpBackend) {
httpBackend = $httpBackend
httpBackend
.expectGET('https://api.github.com/users/hello/repos?access_token=' + gitAccessToken)
.respond(
{ data: items }
);
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
var items = [
{
"name": "test1"
},
{
"name": "test2"
}
];
it('adds search results to array of repo names', function() {
ctrl.username = 'hello';
ctrl.doSearch();
httpBackend.flush();
expect(ctrl.gitRepoNames).toEqual(["test1", "test2"]);
});
});
});
当我运行测试时,我得到错误
应[]等于["test1","test2"]。
很明显,这是因为self.gitRepoNames
没有被填充。当我在测试中的预期之前控制台日志ctrl.repoSearchResult
时,我得到
Object{data: [Object{name: ...}, Object{name: ...}]}
这就是问题所在,因为当在addRepoNames
函数的for循环中调用self.repoSearchResult.length
时,它将是未定义的。
所以我的问题是,为什么response.data
在测试中的addRepoNames
函数中被调用时不返回数组?
如有任何帮助,我们将不胜感激。
编辑:我应该提到的是,该应用程序在服务器上运行时运行良好。
ctrl.doSearch
是一个异步函数。您应该以异步方式处理它。尝试:
it('adds search results to array of repo names', function(done) {
ctrl.username = 'hello';
ctrl.doSearch().then(function() {
expect(ctrl.gitRepoNames).toEqual(["test1", "test2"]);
done();
});
httpBackend.flush();
});
相关文章:
- ReferenceError:Can't在AngularJS中查找变量:xxxService,Karma
- 使用karma浏览器对AngularJS应用程序进行单元测试
- TypeError:无法读取属性'那么'在使用Karma测试AngularJS控制器时未定义的
- AngularJs的单元测试失败是因为Karma没有'无法识别$scope$控制器中的侦听器
- AngularJS / Karma 单元测试与真实的 XHR 数据 / passThrough()
- 控制台.log在Karma AngularJS方法测试中
- 单元测试:AngularJS + Karma + Jasmine,加载本地JSON
- 如何将实际的依赖项加载到带有Jasmine和Karma的Angularjs测试中
- 在 Karma (AngularJS) 中抑制堆栈跟踪
- AngularJS工厂在Karma与Jasmine进行测试
- AngularJS, Karma - 清除 ng-脏与 setPristine.
- 控制器方法在 AngularJs 中使用 Karma 调用 Jasmine 单元测试套件
- 用Karma测试angularjs的工厂
- AngularJS使用Karma进行端到端测试,使用静态文件而不是服务器
- 使用$httpBackend测试Jasmine/Karma的AngularJS不起作用
- Karma + AngularJS +剑道:剑道网格测试不起作用
- Karma AngularJS引导和窗口变量
- Karma AngularJs取消/模拟超时E2E
- 使用karma angularjs在单元测试中注入问题
- 如何将ngRoute注入Jasmine / Karma AngularJS单元测试