Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: spyOn is not defined Angularjs/Karma/Jasmine

I am using AngularJS with karma using the Jasmine framework. I have several other tests running and working. My problem is when I am trying to run this:

spyOn(window, 'confirm').and.returnValue(true);

I get this error:

ReferenceError: spyOn is not defined

Here is my config:

module.exports = function() {
  return {
    basePath: '../',
    frameworks: ['jasmine'],
    reporters: ['progress'],
    browsers: ['Chrome_without_security'],
    autoWatch: true,
    // these are default values anyway
    singleRun: false,
    colors: true,
    plugins : [
            'karma-chrome-launcher',
            'karma-jasmine',
            'karma-ng-scenario'
            ],
    customLaunchers: {
      Chrome_without_security: {
        base: 'Chrome',
        flags: ['--disable-web-security']
      }
    },
    files : [
      'static/js/bower_components/angular/angular.js',
      'static/js/app.js',
      'static/js/controllers.js'
    ]
  }
};

var sharedConfig = require('./karma-shared.conf');

module.exports = function(config) {
  var conf = sharedConfig();

  conf.files = conf.files.concat([
    //test files
    './tests/e2e/account/sign-up.js',
    './tests/e2e/account/sign-in.js',
    './tests/e2e/organization/*.js',
    //'./tests/e2e/**/*.js',
    './tests/e2e/account/sign-out.js'
  ]);

  conf.proxies = {
    '/': 'http://localhost/'
  };

  conf.urlRoot = '/__karma__/';

  conf.frameworks = ['ng-scenario'];

  config.set(conf);
};

The config consists of a shared and specific config for e2e tests.

I have everything else work and Jasmine is specified as the framework in my Karma config. Any ideas?

like image 354
Chris Casper Avatar asked Mar 19 '14 16:03

Chris Casper


1 Answers

Try this inside your test script:

describe('testing spy',function(){
  var window_confirmSpy;

  beforeEach(function(){
    window_confirmSpy = spyOn(window, 'confirm').and.callThrough();
  });

  it('testing windows confirm method',function(){
    window.confirm();

    expect(window_confirmSpy).tohaveBeenCalled();
  });

  it('testing windows confirm method with parameter',function(){
    window.confirm('parameter');

    expect(window_confirmSpy).tohaveBeenCalledWith('parameter');
  });
});

Above code snippet will ensure that window.confirm() is called or not.

like image 140
samyak bhalerao Avatar answered Nov 12 '22 00:11

samyak bhalerao