用玩笑测试chrome.storage.local.set [英] testing chrome.storage.local.set with jest

查看:56
本文介绍了用玩笑测试chrome.storage.local.set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是测试的初学者,开始使用玩笑.我已经将此函数保存在chrome.storage.local中,我想测试chrome.storage.local是否被调用了3次.这是函数:

I'm a very beginner with testing and started using jest. This function I have saves data in chrome.storage.local and I want to test if chrome.storage.local called 3 times. Here is the function:

saveData: function(){
  chrome.storage.local.set({'blackList': bgModule.blackList}, function() {});
  chrome.storage.local.set({'pastDays': bgModule.pastDays}, function() {});
  chrome.storage.local.set({'websiteList': bgModule.websiteList}, function() {});
}

这是我的测试:

const bgModule = require("../../app/Background/background.js");

const chrome = {
  storage: {
    local: {
      set: function() {},
      get: function() {}
    }
  }
};

let blackListStub = bgModule.blackList;
let pastDaysStub = [
  {day: "day1"},
  {day2: "day2"},
  {day3: "day3"}];
let websiteListStub = [
  {websiteName: "facebook.com"},
  {websiteName: "stackoverflow.com"},
  {websiteName: "github.com"}];

it ("should save data in local storage", () => {
  spyOn(bgModule, 'saveData');
  bgModule.saveData();
  const spy = spyOn(chrome.storage.local, 'set');

  spy({'blackList' : blackListStub});
  spy({'pastDays' : pastDaysStub});
  spy({'websiteList' : websiteListStub});

  expect(spy).toHaveBeenCalledWith({'blackList' : blackListStub});
  expect(spy).toHaveBeenCalledWith({'pastDays' : pastDaysStub});
  expect(spy).toHaveBeenCalledWith({'websiteList' : websiteListStub});
  expect(bgModule.saveData).toHaveBeenCalledTimes(1);
  expect(spy).toHaveBeenCalledTimes(3);
});

测试通过,但不正确.我想使其工作,所以当我更改saveData时,它将破坏测试.我进行的测试将所有内容都进行了硬编码,并且完全不依赖saveData.我查找了示例和教程资料,但在这种情况下找不到任何东西.我应该采取的任何帮助或指导都将非常有帮助.

Test passes but it's incorrect. I want to make it work so when I change saveData it will break the test. The test I have has everything hardcoded into it and doesn't depend on saveData at all. I looked for example and tutorial materials and couldn't find anything for this case. Any help or direction I should take would be very helpful.

在使用global.chrome重构测试获得帮助并给出正确答案后,如下所示:

After help and correct answer with use of global.chrome refactored test looks like this:

it ("should save data in local storage", () => {
  bgModule.saveData();
  expect(chrome.storage.local.set).toHaveBeenCalledTimes(3);
});  

推荐答案

最简单的方法是使用依赖注入,因此与其直接调用chrome.storage.local而不是直接将chrome.storage.local传递给函数,然后可以轻松地将其替换为函数在您的测试中通过模拟.

The easiest thing would be to use dependency injection, so instead of calling chrome.storage.local directly, passed it as an argument to your function that then can be easily replaced by a mock in your test.

另一种方法是将其作为全局变量添加到您的测试环境中,如下所示:

The other way would be to add it as a global variable into your test environment like this:

const get = jest.fn()
const set = jest.fn()
global.chrome = {
  storage: {
    local: {
      set,
      get
    }
}

这篇关于用玩笑测试chrome.storage.local.set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆