问题描述
我正在使用Mocha,Chai,Sinn,Proxyquire和Nock.
Am using Mocha, Chai, Sinon, Proxyquire and Nock.
对于这种特殊的测试场景(正在询问此问题),希望多次测试完全相同的URL,每次都在单独的测试中进行测试,期望得到不同的响应.
For this particular test scenario (for which this question is being asked), wish to test the exact same URL several times, each in a separate test that expects a different response.
例如,响应中没有商家供稿,1个商家供稿,又有2个商家供稿.
For example, a response with no merchant feeds, 1 merchant feed, and yet again with 2 merchant feeds.
现有代码都可以正常运行,此外,如果我单独运行测试,则它们可以通过.
The existing code all works, furthermore if I run tests individually they pass.
但是,如果我在单个套件中使用Mocha一起运行它们,它们将失败.相信问题在于,Nock劫持了给定URL的全局http对象,并且每个测试(同时异步运行)正在争夺相同的全局响应引用.
However, if i run them together using Mocha in a single suite they fail. Believe the issue is that Nock hijacks the global http object for a given URL and each test (running asynchronously at the same time) is competing for the same global response reference.
在上述情况下,设置为1个商人的罐头答复准备的响应会被设置覆盖,以2个商人等的响应.
In the scenario above, a response prepared with a canned reply of 1 merchant is getting say overwritten by the setup to respond with 2 merchants etc.
是否有一种避免这种情况发生的机制,例如,保证异步执行Mocha测试用例的序列化(我认为这是默认行为).
Is there a mechanism to avoid this happening, for instance guarantees around serial execution of async Mocha testcases (which I believed was the default behaviour).
推荐答案
好,所以可行(示例代码):
Ok, so this works (sample code):
beforeEach(function (done) {
nock(apiUrl)
.get('/dfm/api/v1/feeds?all=false')
.reply(200, [
{'merchantId': 2, 'id': 2, 'disabled': false}
], { server: 'Apache-Coyote/1.1',
'set-cookie': [ 'JSESSIONID=513B77F04A3A3FCA7B0AE1E99B57F237; Path=/dfm/; HttpOnly' ],
'content-type': 'application/json;charset=UTF-8',
'transfer-encoding': 'chunked',
date: 'Thu, 03 Jul 2014 08:46:53 GMT' });
batchProcess = proxyquire('./batchProcess', {
'./errorHandler': errorHandler.stub,
'./batchTask': batchTask.stub
});
winston.info('single valid feed beforeEach completed');
done();
});
有很多复杂因素.需要注意的两件事:
There were lots of complicating factors. Two things to be aware of:
1).我有一个异步测试用例,但是使用了beforeEach()而没有完成参数.然后,这导致了URL冲突.通过明确声明每个beforeEach(done)并调用done(),Mocha将以串行顺序运行,并且不再存在问题.
1). I had async testcases but was using beforeEach() without the done param. This was then causing the URL collisions. By explcitly declaring each beforeEach(done) and invoking done() Mocha will run in serial order and there is no longer an issue.
2).如果在同一测试套件文件中有多个测试,请确保,如果您在后续测试中声明了相同的URL且具有备用响应,则在上一个测试中设置的所有Nock固定装置实际上都将被执行.如果没有调用先前的nock固定装置,则nock STILL会保留来自错误测试(前一个测试)的响应.这是我的主要问题.您可能会争辩说,如果不运行测试,就应该声明没有任何固定装置-但您也可能会争辩说,这仍然是Nock工作方式中的一个错误.每个测试装置都隔离在各自的describe/beforeEach(done)函数中.
2). Be sure that if you have more than one test in same testsuite file, that any Nock fixtures you set in a previous test actually get executed IF you have declared the same URL in a subsequent test with alternate response. If the prior nock fixture doesn't get invoked then nock STILL retains the response from the wrong test (the previous one). This was my primary problem. You could argue that tests should not have any fixtures declared if they don't get run - but you could also argue this is still a bug in the way Nock works. The test fixtures were each isolated in their own describe / beforeEach(done) functions..
2天后更新...确定要点2).只是再次咬了我一下,我很高兴我写了上面的说明,以提醒自己有关这个难以调试的问题.如果您同时使用Mocha和Nock,请注意此问题!!
Update 2 days later... OK point 2). just bit me again, and I was pleased I wrote the note above to remind myself about this hard to debug issue. If you are using Mocha and Nock together be aware of this issue!!
最终也实现了nock辅助功能,以协助完成此操作(此处为咖啡):
Did eventually implement a nock helper function too, to assist with this (coffeescript here):
global.resetNock = ->
global.nock.cleanAll()
global.nock.disableNetConnect()
然后在beforeEach开始时,只需应用resetNock()
Then at the start of beforeEach just apply resetNock()
这篇关于如何使用Mocha和Nock重新测试相同的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!