【发布时间】:2018-06-19 03:14:05
【问题描述】:
在使用nock.js 的测试期间模拟node Webshot 发送的请求的正确方法是什么?
我尝试使用以下代码将 http://example.com/foo.html 的模拟响应捕获为 foo.png,但模拟似乎不起作用。
const nock = require("nock");
const webshot = require("webshot");
describe("mock webshot request", function(){
this.timeout(20000);
beforeEach(function(){
nock("http://example.com").persist().get("/foo.html").reply(200, "<h1>Foo</h1>");
});
afterEach(function () {
nock.cleanAll();
});
it("captures mocked response", function(done){
webshot("http://example.com/foo.html", "foo.png",function(err) {
nock.isDone();
if(!err) done();
});
});
});
编辑:
解决方案是将模拟的响应正文传递给 Webshot 而不是 url:
webshot("<h1>Foo</h1>", ...
【问题讨论】:
-
你在嘲笑
foo.html,但在测试foor.html。 -
感谢您指出错字。问题现已编辑。