ReferenceError:spyOn未定义Angularjs / Karma / Jasmine

时间:2014-03-19 16:29:41

标签: angularjs jasmine karma-runner

我使用Jasmine框架将AngularJS与karma一起使用。我有其他几个测试运行和工作。我的问题是当我试图运行它时:

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

我收到此错误:

ReferenceError: spyOn is not defined

这是我的配置:

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

配置包含e2e测试的共享和特定配置。

我有其他一切工作,Jasmine被指定为我的Karma配置中的框架。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在测试脚本中尝试:

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

以上代码段将确保调用window.confirm()