茉莉间谍找不到财产

问题描述 投票:1回答:1

我有一个基本上看起来像这样的文件(缩短了)

const octokit = new (require("@octokit/rest"))();
function buildRepo(name) {
  fs.promises
    .readFile("data/settings.json")
    .then(data => JSON.parse(data))
    .then(settings => settings.repositories.find(repo => repo.name === name))
    .then(repo => {
      let repoName = repo.url
        .substring(repo.url.lastIndexOf("/") + 1)
        .slice(0, -4);
      let jobName = repo.name;
      return octokit.repos
        .get({
          owner: "munhunger",
          repo: repoName
        })
        .then(({ data }) => {
          ...
        });
    });
}

module.exports = { buildRepo };

因此,我想针对它从octokit.repos.get函数获得的数据进行测试。但是由于该功能可以访问互联网并查看GitHub存储库,因此我想对其进行模拟。

我有一些使用茉莉花运行的测试,我对此进行了一些阅读,似乎茉莉花应该可以为我模拟这个。

但是,我编写的测试似乎失败。

const builder = require("./index");

describe("spyOn", () => {
  it("spies", () => {
    spyOnProperty(builder, "octokit");
    builder.buildRepo("blitzbauen");
  });
});

出现错误octokit property does not exist。我在这里做错了什么?我需要将octokit添加到module.exports吗?(这似乎很疯狂)

javascript node.js unit-testing testing jasmine
1个回答
0
投票

是,由于您现在仅导出module.exports,因此需要将Octokit添加到buildRepo。其他模块无法直接访问未导出模块中的任何内容,因此如果可以访问,则应将其导出。

或者,您可以使用Jasmine模拟整个Octokit模块,因此可以通过任何脚本调用模拟版本,但是由于我对Jasmine的经验有限,因此我不确定您将如何进行模拟

© www.soinside.com 2019 - 2024. All rights reserved.