0

I want to test my application using Karma . i have configured it and write a simple test suite for checking if a controller is present in my application or not. i am getting the error "Type Error : cannot call method method 'equal ' of undefined. My test suite condition is given below. Please suggest

describe('module present', function() {
    beforeEach(angular.mock.module('demoapp'));
    it('should have a demoCtrl controller', function() {
        expect(demoapp.ProductCtrl).not.to.equal(null);
    });
});

my karma.config is like this

 files : [
      'Scripts/angular.js',
      'Scripts/angular-translate.js',
      'Scripts/angular-translate-loader-static-files.js',
      'Scripts/angular-mocks.js',
      'Scripts/angular-*.js',
      'Test/lib/angular/angular-mocks.js',
      'Scripts/ProjectScript/app.js',
      'Scripts/ProjectScript/DemoData.js',
      'Scripts/ProjectScript/TimerController.js',     
      'Scripts/ProjectScript/**/*.js',
      'Test/unit/**/*.js'

    ],

    exclude : [
      'Scripts/angular-loader.js',
      'Scripts/angular-scenario.js'
    ],

Thanks and regards utpal

5
  • Show us your karma.conf.js Commented Feb 19, 2014 at 9:49
  • @Edwin Dalorzo karma.config file is mentioned above. Commented Feb 19, 2014 at 9:55
  • 1
    not quite sure. but if you are useing jasmine it should be .toBe(null) and not not.to.equal(null) Commented Feb 19, 2014 at 9:59
  • Indeed, I think the right methods are toEqual() and toBe() Commented Feb 19, 2014 at 11:02
  • @glepretre and michael yes you are right. Commented Feb 19, 2014 at 11:10

2 Answers 2

3

Try this, i hope it helps

beforeEach(module('demoapp'));

  var ctrl, scope;
  // inject the $controller and $rootScope services
  // in the beforeEach block
  beforeEach(inject(function($controller, $rootScope) {
    // Create a new scope that's a child of the $rootScope
    scope = $rootScope.$new();
    // Create the controller
       ctrl = $controller('ProductCtrl', {
      $scope: scope
    });
  }));

it('should have a demoCtrl controller', function() {
        expect(ctrl).not.to.equal(null);
    });
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer , but it is returning undefined instead of null because when I am writing the below test specification it is giving the undefined error. it('should check name', function() { expect(scope.name).toBe("utpal"); });
0

Not sure if you are showing your entire karma.conf.js file. At any rate, you should mention the testing framework you want to use in the configuration. You need a attribute saying

frameworks: ["jasmine"]

The following is an example of an entire configuration. This works perfectly.

module.exports = function (config) {
    config.set({
        basepath: '.',
        frameworks: ["jasmine"],
        //list of file patterns to load in the browser
        files: [
            'web/public/lib/jquery/jquery-1.9.1.js',
            'web/public/lib/angular/angular.min.js',
            'web/public/lib/async/async.js',
            'test/client/lib/angular/angular-mocks.js',
            'web/public/lib/angular-ui/*.js',
            'web/public/js/**/*.js',
            'test/client/public/js/**/*.js',
            'test/client/public/js/**/*.coffee'
        ],

        preprocessors: {
            'web/public/js/**/*.js': ['coverage'],
            '**/*.coffee': ['coffee']
        },

        // use dots reporter, as travis terminal does not support escaping sequences
        // possible values: 'dots' || 'progress'
        reporters: ['progress', 'coverage'],

        coverageReporter: {
            type: 'lcov',
            dir: 'coverage/'
        },

        // these are default values, just to show available options

        // web server port
        port: 8089,

        // cli runner port
        runnerPort: 9109,

        //urlRoot = '/__test/';

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

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

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

        // polling interval in ms (ignored on OS that support inotify)
        autoWatchInterval: 0,

        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera
        // - Safari
        // - PhantomJS
        browsers: ['PhantomJS'],

        // Continuous Integration mode
        // if true, it capture browsers, run tests and exit
        singleRun: true

    });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.