2014-06-12 54 views
2

測試後我有角服務,調用Web API方法:Angularjs單位測試不打然後控制器的一部分在AngularJS呼叫服務及使用噶&茉莉

services.service('loginService', function ($http) { this.loginAction = function (userInfo) { console.log(userInfo);

return $http({ 
    method: 'POST', 
    url: 'url of web api service', 
    params: userInfo 

}); 

} });

我有控制器,其調用此服務&中獲取數據,則框

adminApp.controller('loginController', ['$scope','loginService','localStorageService','$location', function ($scope, loginService, localStorageService, $location) { 

$scope.login = function (isValid) { 
    var userData = { 'username': $scope.username, 'password': $scope.password }; 
    if (isValid) { 

     loginService.loginAction(userData).then(

      function (response) { 

         console.log(response);     
         if (response.data.Success) { 
          alert("Success !!!!"); 
          IsAdminMenu = true; 
          localStorageService.clearAll(); 
          localStorageService.set('Token', response.data.Data); 

          $scope = $scope || angular.element(document).scope(); 
          if (!$scope.$$phase) { 
           //this will kickstart angular if to notice the change 
           $scope.$apply(); 
          } 
          $location.path('/dashboard/'); 
         } 
         else { 
          alert(response.data.Data); 
         } 
        }, function (error) { 
         alert('Got the error'); 
        }); 
    } 
} 

} ]);

其工作良好,但,當我使用利用因緣+茉莉寫入單元測試來測試該控制器

describe('LogIn in Progress....', function() { 
var scope, $controller, ctrl, $rootScope;//use this scope in tests 

    //mock Application to allow us to inject our own dependencies 
    beforeEach(module('adminApp')); 
    //mock the controller for the same reason and include $rootScope and $controller 
    beforeEach(inject(function ($rootScope, $controller) { 
    //create an empty scope 
    scope = $rootScope.$new(); 

    //declare the controller and inject our empty scope 
    ctrl = $controller('loginController', { $scope: scope }); 
})); 
// tests start here 
it('Check for Valid LogIn', function() { 

    scope.username = 'admin'; 
    scope.password = 'test12'; 

    scope.login(true); 


    expect(scope.IsAdminMenu).toEqual(true); 
}); 

});

當我運行這個它不會進入。然後控制器的一部分 console.log(response);沒有得到響應

回答

2
describe('myApp', function() { 
    var scope, 
     controller, 
     mockLoginService = {}, 
     mockLocalStorageService = {}, 
     $q, 
     $location; 

    beforeEach(function() { 
     module('myApp'); 
    }); 

    beforeEach(function() { 
     module(function ($provide) { 
      $provide.value('loginService', mockLoginService); 
      $provide.value('localStorageService', mockLocalStorageService); 
     }); 
    }); 

    beforeEach(inject(function ($rootScope, $controller, _$q_, _$location_) { 
     scope = $rootScope.$new(); 
     controller = $controller('loginController', { 
      $scope: scope 
     }); 
     $q = _$q_; 
     $location = _$location_; 
    })); 

    describe('MyCtrl', function() { 
     var loginActionDefer; 

     beforeEach(function() { 
      loginActionDefer = $q.defer(); 
      mockLoginService.loginAction = jasmine.createSpy('loginService.loginAction').andReturn(loginActionDefer.promise); 
      mockLocalStorageService.clearAll = jasmine.createSpy('localStorageService.clearAll'); 
      mockLocalStorageService.set = jasmine.createSpy('localStorageService.set'); 
     }); 

     it('checks for Valid LogIn', function() { 
      var stubName = 'admin', 
       stubPassword = 'test12' 

      scope.username = stubName; 
      scope.password = stubPassword; 

      scope.login(true); 

      expect(mockLoginService.loginAction).toHaveBeenCalledWith({ username: stubName, password: stubPassword }); 

      loginActionDefer.resolve({data: {Success: true}}); 
      scope.$digest(); 

      expect(scope.IsAdminMenu).toEqual(true); 
     }); 
    }); 
}); 

茉莉花1.3 - 嘲笑例子 - http://jsfiddle.net/vB9Kt/1/

+0

我應該在我的測試 – user2089961

+0

必須合併我的代碼以測試並incjet $ Q服務做什麼修改。在這條線loginService.loginAction = jasmine.createSpy( 'loginService.loginAction') –

+0

geetting誤差and.returnValue(action.promise)。我正在使用茉莉花茉莉花版本:1.3.1修訂版1354556913 – user2089961