2015-02-06 65 views
0

我對AngularJS非常新穎。我構建了一個應用程序來了解它的工作原理,現在我已經開始工作了,我正在嘗試學習如何測試我的代碼。我的問題與下面的代碼是,我的工廠模擬代碼失敗,我不確定我出錯的地方。這裏是我得到的錯誤:工廠模擬不在AngularJS測試中被調用

控制器:ProfileCtrl更新測試失敗 預計的間諜更新已被調用。 錯誤:期望的間諜Profile.show被調用。

控制器:ProfileCtrl顯示測試16失敗 預期的間諜顯示已被調用。 錯誤:期望的間諜Profile.show被調用。

廠:

angular.module("profileUpdate").factory "Profile", [ 
    "$resource" 
    "$q" 
    ($resource, $q) -> 
     Profile = -> 
      @service = $resource("https://stackoverflow.com/users/profiles/:id.json", {id: @id}, 'update': {method: 'PATCH', params: {id: '@id'}, headers: {'Content-Type': 'application/json'}}) 
      return 
     Profile::show = (userId) -> 
      @service.get(id: userId) 
     Profile::update = (updatedProfileObject) -> 
      deferred = $q.defer() 
      @service.update(id: updatedProfileObject.id, profile: updatedProfileObject).$promise.then ((data) -> 
      deferred.resolve ("Your profile has been successfully updated!") 
       ),(err) -> 
        deferred.reject ("Oops...something is wrong..try again later.") 
     return deferred.promise 
     return new Profile 
] 

控制器:

angular.module("profileUpdate").controller "ProfileCtrl", [ 
    "$scope" 
    "$routeParams" 
    "Profile" 
    "$route" 
    "$rootScope" 
    "$timeout" 
    ($scope,$routeParams,Profile,$route,$rootScope,$timeout) -> 
     $rootScope.$on "$routeChangeSuccess", -> 
      $scope.profile = Profile.show($routeParams.id) 
      return 
     $scope.range = (min, max, step) -> 
     step = (if (step is `undefined`) then 1 else step) 
     input = [] 
     i = min 
     while i <= max 
      input.push i 
      i += step 
     input 
     $scope.update = -> 
      promise = Profile.update($scope.profile) 
      promise.then((success) -> 
       $scope.alert = success 
       $timeout -> 
        $scope.alert = false 
       , 5000 
      ,(err) -> 
       $scope.error = err 
       $timeout -> 
        $scope.error = false 
       , 5000) 

] 

測試代碼:

"use strict"; 


describe('Controller: ProfileCtrl', function ($provide) { 
    var ProfileCtrl; 
    var $scope; 
    var ProfileMock; 

    //load the controller's module 
    beforeEach(function(){ 
     ProfileMock = { 
      update: function() {} 
     }; 
     module('profileUpdate', function($provide) { 
      $provide.value("Profile", ProfileMock); 
     }); 
    }) 

    beforeEach(inject(function ($controller, $rootScope, Profile) { 
      $scope = $rootScope.$new(); 
      Profile = Profile 
      ProfileCtrl = $controller('ProfileCtrl', { 
       $scope: $scope, 
       Profile: Profile 
      }); 
     })) 




    it('should have 3 items', function() { 
     var things = $scope.range(1,3,1); 
     expect(things.length).toBe(3); 
    }); 
    it('update test', function() { 
    spyOn(ProfileMock, 'update'); 
     $scope.update 
     expect(ProfileMock.update).toHaveBeenCalled(); 
    }); 
    it('show test', function() { 
     spyOn(ProfileMock, 'show'); 

     var $routeParams = { 
       id: 16 
     } 
     $scope.$broadcast('$routeChangeSuccess', $routeParams.id); 
     $scope.$apply() 
     expect(ProfileMock.show).toHaveBeenCalled(); 
    }); 
}); 
+0

它可以親如果你用這個代碼做了一個jsfiddle(或者類似於這個),它可以起到很大的幫助。見例如我的這個存儲庫(https://github.com/liori/mwe-angular)舉例說明了如何在jsfiddle中佈置代碼。 – liori 2015-02-07 02:04:09

回答

0

你沒有在ProfileMock定義show方法,因此不能被稱爲

+3

你能用你擁有的更新代碼嗎? – adamweeks 2015-02-07 02:42:50