2017-12-18 14 views
1

私のコントローラで実行されているサービスコールをテストするためのコードを記述しようとしています。私はサービスコールをしているコントローラの特定の機能をユニットテストし、データを取得しようとしています。現在、私はローカルjsonで試していますが、実際にサービスコールを行います。サービスコールのコントローラをテストできません

私はスパイオブジェクトを作成していますが、「TypeError:jasmine.CreateSpyObjは関数ではありません」というエラーが発生していることを知りました。私は単体テストの初心者です。私はspyobjectを作成することができませんので、さらに進めません。コードを保存して、私に助けてください。

また、spyObjectを正常に作成すると正確に何をしなければならないのか分かりません。実際にサービスが正常に機能しているかどうかをテストしたいのですが、サービスからの応答があります。

私は今これを何日も頑張っています。

サービスコード:

//app is the module name 
app.factory('appServices', ['$rootScope', '$http', function($rootScope, 
$http) { 
var appServices = {}; 
appServices.getData = function(){ 
     return $http.get(scripts/services/data/unitTesting.json'); 
}; 

unitTesting.jsonコード:

{ 
"name":"unit testing", 
"countryCode": "EG", 
"countryName": "Egypt", 
"region": "Africa" 
} 

コントローラコード:

getData: function(){ 
     appServices.getData().then(function(response) { 
      if (response && response.data) { 
       $scope.testVariable= response.data.name; 
      } 
     }); 
    }, 

ユニットテストコード:

describe('myCtrl', function() { 
     beforeEach(module('app')); 

     var $controller; 

     beforeEach(inject(function(_$controller_){ 
     $controller = _$controller_; 
    })); 

describe('service call test', function() { 
var $http,$httpBackend,appServices, 
myService,$q,$rootScope,controller; 

var mockItem = 
     { 
     "name":"unit testing", 
     "countryCode": "EG", 
     "countryName": "Egypt", 
     "region": "Africa" 
     } 
beforeEach(inject(function(_$http_,_$httpBackend_,appServices,_$q_, 
_$rootScope_) { 
    $http = _$http_; 
    $httpBackend = _$httpBackend_; 
    appServices = appServices; 
    $rootScope = _$rootScope_; 
    $q =_$q_; 
    jasmine.spyOn(appServices, 'getData').and.returnValue($q.when(mockItem)); 
    controller = $controller('myCtrl', { $scope: $scope }); 
})); 
    it('Service call test ', function() { 
    controller = $controller('myCtrl', { $scope: $rootScope.new() }); 
    controller.getData(); 
    expect(appServices.getData).toHaveBeenCalled(); 
}); 
}); 
}); 

ERROR:

TypeError: jasmine.spyOn is not a function 
+0

小文字にする必要があります: 'jasmine.createSpyObj' –

+0

私の間違いを助けてくれてありがとう。私のサービスコールが成功したかどうかをテストするためのこれ以上のステップを教えてください。私はオンラインで多くのソリューションを見てきました。あなたがさらなるステップを助けることができるなら、それは素晴らしいことでしょう。 – GiggleGirl

+0

どのバージョンのジャスミンを使用していますか? –

答えて

0

使用spyOn機能:あなたは、その後のようなあなたのテストで通話を確認することができ

jasmine.spyOn(appServices, 'getData'); 

:あなたはあなたのスペックを書かれているかを見てみると

expect(appServices.getData).toHaveBeenCalled(); 

、実行するのに役立ついくつかのその他の変更を提案できますか:

var $rootScope, 
    controller, 
    $q; 

beforeEach(inject(function(_$http_,_$httpBackend_,appServices,_$q_, _$rootScope_){ 
    $http = _$http_; 
    $httpBackend = _$httpBackend_; 
    appServices= appServices; 
    $rootScope = _$rootScope_; 
    $q = _$q_; 
    jasmine.spyOn(appServices, 'getData').and.returnValue($q.when(mockItem)); 
    controller = $controller('myCtrl', { $scope: $scope }); 
})); 
describe('when fetching data in the controller', function(){ 
    it('should delegate the call to appServices.getData()', function() { 
     controller = $controller('myCtrl', { $scope: $rootScope.new() }); 
     controller.getData(); 
     expect(appServices.getData).toHaveBeenCalled(); 
    }); 
}); 
+0

今何がエラーですか? – mindparse

+0

@Vitaliy Gorbenko、私はそれに応じて私の質問を編集した私のユニットテストコードを編集したが、私はまだエラーが表示されます。私のコードを見て、元の質問をエラーメッセージで編集してください。 – GiggleGirl

+0

あなたと別に接続する方法はありますか?私は自分のコードで何か重要なことを見逃していると感じています。 – GiggleGirl

関連する問題