javascript - 忽略/覆盖Sinon错误替换`TypeError:尝试替换已经替换的foo`
问题描述
我已经模拟/替换了对象上的方法。在测试期间,我想替换替换,以便它返回 / async 产生新的东西。反正有没有通过“力”或类似的东西来代替。我知道它已经被替换了,请诗浓,继续用这个替换当前的替换。
import sinon from "sinon";
import * as bar from "./bar";
import * as foo from "./foo";
describe("availabilityWorker", () => {
describe("sync", () => {
let sandbox: sinon.SinonSandbox;
beforeEach(async () => {
sandbox = sinon.createSandbox();
sandbox.replace(bar, "bar", () => {});
sandbox.replace(foo, "foo", async function* () {
yield { error: undefined, data: { hello: "world" } };
});
});
afterEach(() => {
sandbox.restore();
});
it("should do what I tell it", async () => {
{
// part 1 - (throws errors here )
sandbox.replace(foo, "foo", async function* () {
yield { error: undefined, data: { hello: "world2" } };
});
// ... assertions
}
{
// part 2
sandbox.replace(foo, "foo", async function* () {
yield { error: undefined, data: { hello: "world3" } };
});
// ... assertions
}
});
});
});
注意我不想这样做,sandbox.restore()
因为这会恢复一切,我只想覆盖foo
解决方案
推荐阅读
- file - 如何使用java从Android 10的外部存储中获取所有带有FileName的PDF文件Uri
- azure - Azure 持久函数:获取执行时间
- ios - 如何使用 CloudKit 保存图像?
- ios - Swift Realm,当应用程序在后台时,写入数据库在appdelegate中不起作用
- javascript - 如何将某些 Date 对象转换为德国的相应时间?
- xml - 针对 XSD 架构验证 XML
- node.js - 通过 Admin SDK for Google Cloud Identity Platform 创建默认提供程序
- javascript - 在 apache 服务器上部署 React (3000) 和 Express (8000) 应用程序
- javascript - 使用 jest 比较两个文件的内容
- c++ - 在 C++11 中,可以在同一行声明全局变量和函数原型吗?