【发布时间】:2017-02-27 21:16:06
【问题描述】:
我无法让测试结果通过我正在使用一个非常基本的实现来更深入地了解测试。
我有一个工厂,它返回一个承诺,从我的控制器访问。我想测试调用是否成功并将响应分配给repos var。以下是代码:
'use strict';
angular.module('app')
.factory('searchServ', function ($timeout, $q, $http) {
return {
fetch: function(user) {
var deferred = $q.defer();
$timeout(function(){
$http({method: 'GET', url: 'https://api.github.com/users/' + user + '/repos'}).then(function(repos) {
deferred.resolve(repos.data);
}, function(reason){
deferred.reject(reason.status);
console.log(reason);
});
}, 30);
return deferred.promise;
}
};
})
.controller('MainCtrl', function ($scope, searchServ) {
$scope.results = function(user) {
$scope.message = '';
searchServ.fetch(user).then(function (repos) {
if(repos.length){
$scope.message = '';
$scope.repos = repos;
}
else{
$scope.message = 'not found'
}
}, function (){
$scope.message = 'not found';
});
};
});
//Test
'use strict';
describe('MainCtrl', function () {
var scope, searchServ, controller, deferred, repos = [{name: 'test'}];
// load the controller's module
beforeEach(module('app'));
beforeEach(inject(function($controller, $rootScope, $q) {
searchServ = {
fetch: function () {
deferred = $q.defer();
return deferred.promise;
}
};
spyOn(searchServ, 'fetch').andCallThrough();
scope = $rootScope.$new();
controller = $controller('MainCtrl', {
$scope: scope,
fetchGithub: fetchGithub
});
}));
it('should test', function () {
expect(scope.test).toEqual('ha');
});
it('should bind to scope', function () {
scope.results();
scope.$digest();
expect(scope.message).toEqual('');
//expect(scope.repos).not.toBe(undefined);
});
});
运行测试给我以下错误:
TypeError: undefined is not a function (evaluating 'spyOn(searchServ, 'fetch').andCallThrough()') in test/spec/controllers/main.js (line 15)
知道如何测试它以测试范围绑定以及异步调用吗?
【问题讨论】:
-
没有
andCallThrough。它是and.callThrough。您广泛使用deferred antipattern。在你的情况下结果会很糟糕。因为 searchServ.fetch 在您的情况下返回未解决的承诺。 -
我已经尝试过 and.callThrough 但它总是返回未定义,我也在尝试另一种方法,比如这个小提琴jsfiddle.net/upoc0ott/2 但仍然未定义,如果有人可以帮助我使用,那就太棒了无论哪种方式?
标签: angularjs unit-testing asynchronous jasmine karma-jasmine