我只是想知道是否有办法模拟一个"没有连接"摩卡单元测试中的事件。我有一个方法返回一个承诺,应该等待与轮询策略的互联网连接,我想用mocha测试它。有一种方法可以达到这样的效果。
一些代码:
it('should wait for connection', function(done)
{
this.timeout(2500);
//simulate an internet connection drop here
wait.connection().then(done);
setTimeout(//activate connection, 2000);
});
感谢您的回复。
[编辑]:这里有我要测试的代码:
var wait = {
connection: function()
{
'use strict';
var settings = {retry: 1000};
return new Promise(function(resolve)
{
(function poll()
{
needle.get('https://api.ipify.org', function(error, response)
{
if(!error && response.statusCode === 200 && /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/.test(response.body))
resolve();
else
wait.for(settings.retry).then(poll);
});
})();
});
}
};
在这种情况下,Promise将保持挂起状态,因为新的连接可用。如何在needle
获取请求中模拟错误?
答案 0 :(得分:3)
您可以使用sinon来存根needle.get
var needle = require("needle");
var sinon = require("sinon");;
before(() => {
sinon.stub(needle, "get", (url, calback) => {
var dummyError = {};
callback(dummyError);
})
});
// Run your 'it' here
after(() => {
needle.get.restore();
});
答案 1 :(得分:0)
您可以使用Private Function GeneratePDF(ByVal streamFO As IO.Stream, ByVal pdfFile As String) As Boolean
'OLD Dim streamFO As java.io.FileInputStream = New java.io.FileInputStream(foFile)
Dim memStreamFo As ApacheFop.Net.tools.DotNetInputStream = New ApacheFop.Net.tools.DotNetInputStream(streamFO)
Dim src As org.xml.sax.InputSource = New org.xml.sax.InputSource(memStreamFo)
Dim streamOut As java.io.FileOutputStream = New java.io.FileOutputStream(pdfFile)
Dim oDriver As org.apache.fop.apps.Driver = New org.apache.fop.apps.Driver(src, streamOut)
oDriver.setRenderer(org.apache.fop.apps.Driver.RENDER_PDF)
Try
oDriver.run()
BCreated = True
Catch ex As Exception
BCreated = False
Finally
streamOut.close()
streamFO.close()
oDriver.reset()
GC.Collect()
End Try
End Function
:https://github.com/nock/nock#enabledisable-real-http-requests
答案 2 :(得分:-2)
如果您没有连接或任何连接问题,请使用您的承诺的2015-04-…'
并抓住它们。 reject()
是一个函数,如果使用非null参数调用该函数,则表示您的测试不好。
也许你必须尝试:
done