2016-11-15 106 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()的傳遞,對吧?將代碼更改爲你的代碼後,它仍然給我兩個函數相同的錯誤。 –