我正在使用 Mocha、Chai 和 Nock 编写单元测试代码来模拟 HTTP 请求。
所有测试都没有问题地通过,但它显示错误“错误:连接 ECONNREFUSED 127.0.0.1:80”和“在套接字实例上发出‘错误’事件”
这是代码:
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import nock from 'nock';
use(chaiAsPromised);
beforeEach(() => {
foo = () => new Foo(`${hostname}/api/lock`);
nock.disableNetConnect();
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
context('lockAsync()', () => {
it('Should return true', async () => {
// Arrange
const foo = foo();
// Act
const expiration = new Date('2020/01/01 10:00:00');
const scope = nock(hostname)
.post('/api/foo/bar/', expiration.toDateString())
.reply(201, 'true');
const result= await foo.lockAsync('value1', 'value2', 'value3', expiration);
scope.done();
// Assert
expect(result).to.be.true;
});
});
// Other similar tests below
异常由:C:\Users\...\node_modules\mocha\lib\runner.js:962
Runner.prototype._uncaught = function(err) {
// this is defensive to prevent future developers from mis-calling this function.
// it's more likely that it'd be called with the incorrect context--say, the global
// `process` object--than it would to be called with a context that is not a "subclass"
// of `Runner`.
...
if (this.state === constants.STATE_STOPPED) {
debug('uncaught(): throwing after run has completed!');
throw err;
}
...