【发布时间】:2015-01-28 22:41:27
【问题描述】:
我正在尝试为我的代码编写单元测试,我需要一些指导。
我的文件中有类似的东西
//inside my 'testCtrl' I have
$scope.calculateTime = function() {
var date = new Date();
$scope.currentYear = date.getFullYear();
}
$scope.calculateLastYear = function() {
$scope.currentYear = $scope.currentYear - 1;
}
我的测试文件。
describe('Controller: testCtrl', function(){
beforeEach(module('myApp'));
beforeEach(inject(function(_$controller_, _$rootscope_) {
scope._$rootScope.$new();
testCtrl = _$controller_('testCtrl', {
$scope:scope
})
})
//for some reason, every tests I write below are passed even
//though it should fail
it('should get the last year'), function() {
expect(scope.currentYear).toBe('text here….') //<-- it should fail but
//it passes
};
})
我不知道如何编写测试来检查calculateLastYear 函数,我不知道为什么我的expect(scope.currentYear).toBe('text here….') 通过了。任何人都可以帮助我吗?非常感谢!
【问题讨论】:
-
beofreEach 应该是 beforeEach
-
括号放错了地方。 it('应该得到最后一年'),
-
不知道为什么这是题外话 - 有一个错字 - 但语法也有问题。
标签: javascript angularjs unit-testing karma-runner