私はテスト結果を得ることができません私は非常に基本的な実装を使用して、より深いテストを理解しています。ユニットテストは、約束を使用する角度コントローラとサービスですか?
私は私のコントローラからアクセスして約束を返す工場を持っています。私は呼び出しが成功したことをテストし、応答を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'です。あなたは[遅延反パターン](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern)を広く使用しています。あなたの場合はひどく不利になるでしょう。あなたのケースでは、searchServ.fetchは未解決の約束を返します。 – estus
私はand.callThroughを試しましたが、いつも未定義を返します。私はこのフィドルのような別のアプローチを試みていました。https://jsfiddle.net/upoc0ott/2/でも未定義ですいずれかの方法を使用してアウト? – SinSync