我开始使用 karma 进行一些单元测试,但我不明白为什么我的第一个测试没有通过。
这是一个 Controller 文件:
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) Requests controller should have proper requestTypesList value FAILED
Expected
[ Object({ name: 'New', trigram: 'NEW' }), Object({ name: 'Enhancement', trigram: 'ENH' }) ]
to be
[ Object({ name: 'New', trigram: 'NEW' }), Object({ name: 'Enhancement', trigram: 'ENH' }) ].
at Object. (C:/Users/aazor102115/Desktop/Dev/Balrog/tests/requests.js:28:35)
Chrome 43.0.2357 (Windows 7 0.0.0) Requests controller should have proper requestPrioritiesList value FAILED
Expected [ Object({ name: 'Low', trigram: 'LOW' }), Object({ name: 'Medium', trigram: 'MED' }), Object({ name: 'High', trigram: 'HIG' }) ]
to be
[ 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): Executed 2 of 2 (2 FAILED) ERROR (0.12 secs / 0.105 secs)
因此,即使日志中的值相同,测试也会失败。为什么会这样以及如何解决它?
最佳答案
将您的 toBe
断言更改为 toEqual
,它们在内存中不是同一个对象。
关于javascript - karma 测试因良好的值(value)观而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775591/