Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs/mocha/nock-模拟整个html响应_Node.js_Unit Testing_Express_Mocking_Mocha.js - Fatal编程技术网

Node.js nodejs/mocha/nock-模拟整个html响应

Node.js nodejs/mocha/nock-模拟整个html响应,node.js,unit-testing,express,mocking,mocha.js,Node.js,Unit Testing,Express,Mocking,Mocha.js,如何为测试模拟整个HTML主体响应 我用的是nodejs/mocha/nock 使用nock,我可以很好地模拟JSON响应,例如: nock('http://myapp.iriscouch.com') .get('/users/1') .reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte'}); 我使用curl-o获取我想要的用于模拟的html,因此我已经将其保

如何为测试模拟整个HTML主体响应

我用的是nodejs/mocha/nock

使用nock,我可以很好地模拟JSON响应,例如:

nock('http://myapp.iriscouch.com')
                .get('/users/1')
                .reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte'});
我使用
curl-o
获取我想要的用于模拟的html,因此我已经将其保存在一个文件中,但是我不知道如何将html文件传递给nock(或其他东西)


谢谢。

首先获取测试文件的HTML内容并将其放入字符串中(例如使用
fs.readFile

之后,您可以执行以下操作:

nock('http://myapp.iriscouch.com').
        get('/users/1').
        reply(200, yourFileContent);
这就是我过去的做法:)

如果愿意,可以显式指定内容类型,因为将主体指定为字符串,这将有效地让您轻松模拟任何非二进制响应:

nock('http://myapp.iriscouch.com').
        get('/users/1').
        reply(200, yourFileContent, {'content-type': 'text/html'});
如果你想要一个更一般的方法,我问了一个类似的问题,得到了一些有趣的回答