2016-11-15 107 views
1

我试图测试一个控制器功能,但噶无法如预期地捡起它。我遵循了指令:https://docs.angularjs.org/guide/controller

这里是我的代码:

var app = angular.module('calApp', []); 
app.controller('calCtrl', function($scope) { 
    $scope.sum = function(x, y) { 
     return x + y; 
    }; 
}); 
describe('calCtrl function', function() { 
    describe('calCtrl', function() { 
     var $scope; 
     beforeEach(module('calApp')); 
     beforeEach(inject(function($rootScope, $controller) { 
      $scope = $rootScope.new(); 
      $controller('calCtrl', {$scope: $scope}); 
     })); 
     it('should add 2 numbers correctly', function() { 
      expect($scope.sum(1, 2)).toBe(3); 
     }); 
     it('should subtract 2 numbers correctly', function() { 
      expect($scope.subtract(5, 3)).toBe(2); 
     }); 
    }); 
}); 

我希望测试给1张通行证为$ scope.sum(),1次失败的$ scope.substract()。请帮忙!

错误: 类型错误:未定义不是在测试中的物体(评价 '$ scope.subtract')/ spec.js

另外: 执行的2的2(2 FAILED)

业。 conf.json:

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
    'lib/angular.js', 
    'lib/angular.min.js', 
    'lib/angular-mocks.js', 
    'public_html/*.html', 
    'public_html/*.js', 
    'tests/*.js' 
    ], 


    // list of files to exclude 
    exclude: [ 
    "**/angular-scenario.js" 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
    }, 


    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['PhantomJS'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 

    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity 
}) 
} 

更新:我也有失败的期望(真).toBe(真)

+0

那么发生了什么?你得到什么错误? – bhantol

+0

对不起,我忘了补充一点。现在已经开始。 –

+0

我也注意到你有angular.js两次定义'lib/angular.js'和'lib/angular.min.js', – bhantol

回答

0

你不必定义了。

定义subtract

app.controller('calCtrl', function($scope) { 
    $scope.subtract= function(x, y) { 
     return x - y; 
    }; 
    $scope.sum = function(x, y) { 
     return x + y; 
    }; 
}); 

或者你可以写在下面的方式减: -

it('should subtract 2 numbers correctly', function() { 
    expect($scope.subtract).toBeDefined(); 
    expect($scope.subtract(5, 3)).toBe(2); 
}); 

更新基于错误:

你beforeEach有问题

var $controller; 
beforeEach(inject(function(_$controller_) { 

     $controller = _$controller_; 

    })); 

然后再在你的测试创建:

it('should add 2 numbers correctly', function() { 
    var $scope = {}; 
    var controller = $controller('calcCtrl', {$scope:$scope}); 
    expect($scope.sum(1, 2)).toBe(3); 
}); 

按照ANgularJS Unit Testing Guite

+0

但是,它应该给我一个$ scope.sum()的传递,对吧?将代码更改为你的代码后,它仍然给我两个函数相同的错误。 –