【问题标题】:ReferenceError: spyOn is not defined Angularjs/Karma/JasmineReferenceError: spyOn 未定义 Angularjs/Karma/Jasmine
【发布时间】:2015-04-21 20:27:09
【问题描述】:

我在 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 配置中被指定为框架。有什么想法吗?

【问题讨论】:

  • ng-scenario 语法与 Jasmine 的相似,但并不完全相同。你确定 Jasmine 真的被加载了吗?你是想在单元测试还是集成测试中窥探?
  • @chris Casper,有什么回应吗?

标签: angularjs jasmine karma-runner


【解决方案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');
  });
});

上面的代码 sn-p 将确保 window.confirm() 是否被调用。

【讨论】:

    猜你喜欢
    • 2015-09-03
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2022-12-31
    • 2021-10-08
    • 1970-01-01
    • 2017-09-25
    • 2015-02-10
    相关资源
    最近更新 更多