2014-02-16 2 views
0

내 로그인 컨트롤러에 예상되는 일을하지 않는 것은 :카르마 테스트가

angular.module('mean').controller('LoginController', ['$scope', '$location', '$rootScope', 'UserService', function ($scope, $location, $rootScope, UserService) { 
    $scope.init = function() { 
    $scope.credentials = {}; 
    $scope.status_object = { 
     text: '', 
     class: '', 
     show: false 
    };  
    } 

    $scope.authenticate = function() { 
    $scope.status_object = { 
     text: 'Authenticating...', 
     class: 'info', 
     show: true 
    }; 
    UserService.authenticate($scope.credentials).then(function(response) { 
     if(response.data.status === 'error') { 
     $scope.status_object = { 
      text: response.data.error, 
      class: 'danger', 
      show: true 
     }; 
     } else if(response.data.status === 'ok') { 
     $rootScope.globals.logged_in = true; 
     $location.path('/' + response.data.user.access); 
     } 
    }); 
    }; 
}]); 

내 시험은 다음과 같습니다

내보기에서
describe('LoginController', function() { 
    beforeEach(module('mean')); 

    var scope, rootScope, LoginController, $httpBackend, $location; 

    beforeEach(inject(function($controller, $rootScope, _$httpBackend_, _$location_) { 
     scope = $rootScope.$new(); 
     rootScope = $rootScope.$new(); 

     LoginController = $controller('LoginController', { 
     $scope: scope, 
     $rootScope: rootScope 
     }); 

     $httpBackend = _$httpBackend_; 

     $location = _$location_; 
    })); 

    it('should show danger when wrong credentials are used', function() { 
     scope.credentials = { 
     email: '[email protected]', 
     password: 'password' 
     } 

     $httpBackend.expectPOST('/api/v1/user/auth').respond({ 
     status: 'error', 
     error: 'Invalid User' 
     }); 

     console.log(scope.status_object); 
     scope.authenticate(); 
     $httpBackend.flush(); 


     expect(scope.status_object).toEqualData({text: 'Invalid User', class: 'danger', show: true}); 
    }); 
    }); 

, 내가 ng-init="init()" 있습니다. 모든 것이 정상적으로 작동하지만 테스트를 실행하면 다음과 같이 표시됩니다.

PhantomJS 1.9.7 (Mac OS X) LoginController should show danger when wrong credentials are used FAILED 
    TypeError: 'undefined' is not a function (evaluating 'expect(scope.status_object).toEqualData({text: 'Invalid User', class: 'danger', show: true})') 
     at /myapp/test/karma/unit/controllers/login.spec.js:43 
PhantomJS 1.9.7 (Mac OS X): Executed 4 of 4 (1 FAILED) (0.135 secs/0.043 secs) 

내 테스트가 잘못되어 있는지 확실하지 않습니다. 어떤 아이디어?

답변

2

줄 바꿈 : expect(scope.status_object).toEqualData({text: 'Invalid User', class: 'danger', show: true});이 오류의 원인입니다. 아무 toEqualData 없으므로 Angular tutorial가 않습니다, 길을 내가 대신 toEqual를 사용하고 있는데이

4

예, 당신은 예를 들어, 첫 번째 toEqualData를 정의 할 필요가 지금 일하고있어 :

beforeEach(function(){ 
    this.addMatchers({ 
     toEqualData: function(expected) { 
     return angular.equals(this.actual, expected); 
     } 
    }); 
    }); 
관련 문제