Angularjs 角isn中的业力测试';I don’’我不会做人们期望做的事

Angularjs 角isn中的业力测试';I don’’我不会做人们期望做的事,angularjs,karma-runner,Angularjs,Karma Runner,我的登录控制器是: angular.module('mean').controller('LoginController', ['$scope', '$location', '$rootScope', 'UserService', function ($scope, $location, $rootScope, UserService) { $scope.init = function() { $scope.credentials = {}; $scope.status_ob

我的登录控制器是:

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: 'test@email.com',
        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)

我不确定我的测试出了什么问题。有什么想法吗?

结果是:
expect(scope.status\u object).toEqualData({text:'Invalid User',class:'danger',show:true})导致了错误。没有
toEqualData
,所以我使用
toEqual
,它现在工作了结果是:
expect(scope.status\u object)。toEqualData({text:'Invalid User',class:'danger',show:true})导致了错误。没有
toEqualData
,因此我使用
toEqual
,它现在正在工作是的,您需要首先定义toEqualData,例如,它的工作方式:

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

是的,您需要首先定义toEqualData,例如,定义的方式:

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