Javascript 如何解决我的案例中的单元测试问题

Javascript 如何解决我的案例中的单元测试问题,javascript,angularjs,unit-testing,karma-runner,Javascript,Angularjs,Unit Testing,Karma Runner,我正试图为我的代码编写一个单元测试,我需要一些指导 我的档案里有这样的东西 //inside my 'testCtrl' I have $scope.calculateTime = function() { var date = new Date(); $scope.currentYear = date.getFullYear(); } $scope.calculateLastYear = function() { $scope.currentYear = $scope

我正试图为我的代码编写一个单元测试,我需要一些指导

我的档案里有这样的东西

//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
      };
})
description('Controller:testCtrl',function(){
在每个模块之前(模块(‘myApp’);
beforeach(注入函数($controller,$rootscope){
作用域。$rootScope。$new();
testCtrl=\$controller\'testCtrl'{
$scope:scope
})
})
//出于某种原因,我在下面写的每一个测试都通过了
//虽然它应该失败
它('should get last year')、函数(){

expect(scope.currentYear).toBe('text here…)/您的规范语法不正确。应该是这样的(请原谅双关语):

计算去年规格:

  it('should get the last year', function() {
    $scope.currentYear = 2015;
    $scope.calculateLastYear();
    expect($scope.currentYear).toEqual(2014);
  });

BeofreReach应该放在错误的地方。它('should get the last year'),不确定为什么这是离题的-有一个拼写错误-但是语法也有一个问题。谢谢,我错过了它。你能帮我写CalculateLast函数的测试吗?+1it('should calculate last year',function()){$scope.currentYear=2015;$scope.calculateElastyear();expect($scope.currentYear.toEqual(2014);});
  it('should get the last year', function() {
    $scope.currentYear = 2015;
    $scope.calculateLastYear();
    expect($scope.currentYear).toEqual(2014);
  });