Chr*_*per 6 jasmine angularjs karma-runner
我使用Jasmine框架将AngularJS与karma一起使用.我有其他几个测试运行和工作.我的问题是当我试图运行这个:
spyOn(window, 'confirm').and.returnValue(true);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
ReferenceError: spyOn is not defined
Run Code Online (Sandbox Code Playgroud)
这是我的配置:
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);
};
Run Code Online (Sandbox Code Playgroud)
配置包含e2e测试的共享和特定配置.
我有其他一切工作,Jasmine被指定为我的Karma配置中的框架.有任何想法吗?
小智 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');
});
});
Run Code Online (Sandbox Code Playgroud)
上面的代码片段将确保window.confirm()是否被调用。