Karma测试失败的价值很高?

时间:2015-11-18 09:00:05

标签: javascript angularjs unit-testing karma-jasmine

我开始使用业力进行一些单元测试,但我无法弄清楚为什么我的第一次测试没有通过。

这是一个控制器文件:

angular.module('balrogApp.requests', [
  /* Dependancies */
])
  // Routes configuration
  .config(['$routeProvider', function($routeProvider) {
    /* $routeProvider configuration */
  }])
  .controller('requestsController', function(Requests, Users, Projects, RequestsComments, CostEstimations,
                                             Regions, growl, $route, $rootScope, $scope, $location) {
    this.requestTypesList = [
  {name: "New", trigram: "NEW"},
  {name: "Enhancement", trigram: "ENH"}
];
     this.requestPrioritiesList = [
  {name: "Low", trigram: "LOW"},
  {name: "Medium", trigram: "MED"},
  {name: "High", trigram: "HIG"}
];

/* ... */

});

这是测试文件:

describe('Requests controller', function() {
  beforeEach(module('balrogApp.requests'));

  var ctrl;
  var scope;

  var requestTypesList = [
    {name: "New", trigram: "NEW"},
    {name: "Enhancement", trigram: "ENH"}
  ];

  var requestPrioritiesList = [
    {name: "Low", trigram: "LOW"},
    {name: "Medium", trigram: "MED"},
    {name: "High", trigram: "HIG"}
  ];

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

  afterEach(function() {
    scope.$destroy();
  });

  it('should have proper requestTypesList value', function(){
    expect(ctrl.requestTypesList).toBe(requestTypesList);
  });

  it('should have proper requestPrioritiesList value', function(){
    expect(ctrl.requestPrioritiesList).toBe(requestPrioritiesList);
  });
});

但这是测试结果:

  

Chrome 43.0.2357(Windows 7 0.0.0)请求控制器应具有   正确的requestTypesList值FAILED

     

预期

     

[Object({name:'New',trigram:'NEW'}),Object({name:   '增强',trigram:'ENH'})]

     

     

[Object({name:'New',trigram:'NEW'}),Object({name:   '增强',三元组:'ENH'})]。

     

at Object。   (C:/Users/aazor102115/Desktop/Dev/Balrog/tests/requests.js:28:35)

     

Chrome 43.0.2357(Windows 7 0.0.0)请求控制器应具有   适当的requestPrioritiesList值FAILED

     

预期[Object({name:'Low',trigram:'LOW'}),Object({name:   'Medium',trigram:'MED'}),Object({name:'High',trigram:'HIG'})   ]

     

     

[Object({name:'Low',trigram:'LOW'}),Object({name:'Medium',   trigram:'MED'}),Object({name:'High',trigram:'HIG'})]。

     

at Object。   (C:/Users/aazor102115/Desktop/Dev/Balrog/tests/requests.js:32:40)   Chrome 43.0.2357(Windows 7 0.0.0):执行2 of 2(2 FAILED)ERROR   (0.12秒/0.105秒)

因此,即使日志中的值相同,测试也会失败。为什么这样以及如何解决它?

1 个答案:

答案 0 :(得分:1)

toBe断言更改为toEqual,它们与内存中的对象不同。