繁体   English   中英

jasmine 间谍未找到财产

[英]jasmine spy not finding property

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

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 function 获得的数据。 但是由于 function 将 go 发布到 Internet 并查看 GitHub 存储库,我想模拟它。

我有一些使用 jasmine 运行的测试,我稍微阅读了一下,似乎 jasmine 应该能够为我模拟这个。

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

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

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

带有错误octokit property does not exist 我在这里做错了什么? 我需要将octokit添加到module.exports吗?(这看起来很疯狂)

是的,您需要将 Octokit 添加到module.exports ,因为您现在只导出buildRepo 模块中未导出的任何内容都不能被其他模块直接访问,因此如果应该可以访问,则应该将其导出。

或者,您可以使用 Jasmine 模拟整个 Octokit 模块,因此任何脚本的任何调用都会对模拟版本进行,但我不确定您是如何做到的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM