我正在尝试为我的代码编写单元测试,我需要一些指导。
我的文件中有一些内容,如
//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….')
通过了。任何人都可以帮我吗?非常感谢!
答案 0 :(得分:2)
您的规范语法不正确。它应该是这个(借口双关语):
it('should get the last year', function() {
expect(scope.currentYear).toBe('text here….');
});
计算去年的规格:
it('should get the last year', function() {
$scope.currentYear = 2015;
$scope.calculateLastYear();
expect($scope.currentYear).toEqual(2014);
});