title: Error: spyOn could not find an object to spy upon for login()
body: <p>I am trying on unit test for login using karma and jasmine. I got the following error when I test my code using karma start unit-tests.conf.js.</p>
  
  <p>PhantomJS 1.9.8 (Mac OS X 0.0.0) LoginController doLoginAction should call login method on UserService FAILED</p>
  
  <p>Error: spyOn could not find an object to spy upon for login()</p>
  
  <p>Here I am including my login.controller.tests.js page.</p>
  
  <pre><code>describe('LoginController', function() {
  
      var scope, controller, userServiceMock, stateMock;
  
      beforeEach(module('user.controllers'));
  
      beforeEach(inject(function($rootScope, $controller) {
          scope = $rootScope.$new();
          controller = $controller('LoginController',{
              $scope: scope,
              $state: stateMock,
              UserService: userServiceMock
          });
      }));
  
      beforeEach(function() {
          scope.doLoginAction = jasmine.createSpy('doLoginAction')
      });
  
      describe('doLoginAction',function(){
          it('should call odLoginAction method on LoginController', function(){
              scope.doLoginAction();
          });
  
          it('should call login method on UserService', function(){            
              spyOn(userServiceMock,'login');
              expect(userServiceMock.login).toHaveBeenCalledWith({
                  username:'riju@gmail.com',
                  password:'riju'
              });                
          });
      });        
  });
  </code></pre>
  
  <p>doLoginAction function in my controller page.</p>
  
  <pre><code>$scope.doLoginAction = function () {
      UserService.login($scope.creds.username, $scope.creds.password)
          .then(function (_response) {
  
              alert("login success " + _response.attributes.username);
  
              // transition to next state
              $state.go('tab.list');
  
          }, function (_error) {
              alert("error logging in " + _error.message);
          })
  };
  </code></pre>
  
  <p>What I am doing wrong, please help me.
  thanks</p>