模拟chrome.storage.local函数使用Jasmine
Mock chrome.storage.local functions using Jasmine
我在一个名为options.js
的文件中有以下代码。我需要用茉莉来嘲笑它。我需要测试在调用save
函数时是否调用了chrome.storage.local.set
。
在规范文件中,我有以下代码。但是它没有调用options.js
文件中的chrome函数。有人能指出错误吗?
beforeEach(function() {
ctrl = new OptionCtrl(scope);
chrome: {
storage: {
local: {
set: function() {
}
}
}
}
});
it('should call storage on save', function() {
spyOn(chrome.storage.local, 'set').and.callThrough();
ctrl.save({data: 'check'}, 'check');
expect(chrome.storage.local.set).toHaveBeenCalled();
});
更新1:保存功能实现
save(data, successMessage) {
chrome.storage.local.set(data, (error) => {
if (error) {
this.status('Error Occurred. Please refresh.', 1000, 100, 'danger');
} else {
this.status(successMessage, 1000, 100, 'success');
}
});
}
链接到options.js文件-行:122
茉莉花中的Stubbing很容易,因为:
var chrome = {
storage: {
local: {
set: function() {}
}
}
}
function OptionCtrl() {
this.save = function(data, callbackStr) {
chrome.storage.local.set(data)
}
}
describe('OptionCtrl', function() {
var ctrl;
beforeEach(function() {
ctrl = new OptionCtrl({});
});
it('calls storage on save', function() {
spyOn(chrome.storage.local, 'set').and.callThrough();
var dataStub = {
data: 'check'
}
ctrl.save(dataStub, 'check');
expect(chrome.storage.local.set).toHaveBeenCalledWith(dataStub);
});
})
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
相关文章:
- 模拟chrome.storage.local函数使用Jasmine
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- 为什么spyOn"停止函数“”的所有执行;Jasmine(要求澄清Jasmine 2.2间谍文件)
- 可以't在Jasmine中调用Javascript函数
- 使用Jasmine捕获传递给不同JavaScript文件中函数的参数
- Jasmine:测试setTimeout函数会抛出一个错误
- Jasmine中返回Deferred的模拟嵌套函数
- 使用jasmine测试JavaScript回调函数
- Jasmine测试工厂函数调用本地函数和另一个函数-获取错误:应该是间谍,但得到了function
- Jasmine间谍的函数称为一个方法
- 嵌套描述函数中对象的单个mock(jasmine
- 如何在角度指令规范中使用 jasmine 存根 jquery 函数
- 在 Jasmine 测试中无法访问 Angular 服务函数
- 如何在Jasmine中运行js函数
- Jasmine在全局窗口对象上创建本地JS函数的Spy
- 在jasmine描述函数中,自动向beforeEach和afterEach添加代码
- 如何使用jasmine AJAX存根AJAX函数
- 无法在jasmine it函数中将未定义或null转换为对象
- 如果有's是一个名为“”的全局函数;描述“;或“;它“;当使用诸如jasmine或mocha之类的测试框架时
- Jasmine测试url的javascript函数