Minh Mai
Minh Mai

Reputation: 35

Karma is not getting my $scope from controller

I'm trying to test a controller function however Karma cannot pick it up as expected. I have followed instruction from:https://docs.angularjs.org/guide/controller

Here is my code:

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);
        });
    });
});

I expect the test to give 1 pass for $scope.sum() and 1 fail for $scope.substract(). Please help!

Error: TypeError: undefined is not an object (evaluating '$scope.subtract') in tests/spec.js

Also: Executed 2 of 2 (2 FAILED)

karma.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
})
}

UPDATE: I also have a FAILED for expect(true).toBe(true)

Upvotes: 1

Views: 320

Answers (1)

bhantol
bhantol

Reputation: 9616

You don't have subtract defined.

Define subtract

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

OR you can write the subtract in following way:-

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

UPDATED based on errors:

Your beforeEach has problem

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

      $controller = _$controller_;

    }));

Then further in your tests you create:

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

Follow ANgularJS Unit Testing Guite

Upvotes: 0

Related Questions