How can I write a unit test for this method?
import { CoreOptions, UriOptions } from 'request';
import requestPromise from 'request-promise-native';
export class Client {
public async post(request: CoreOptions & UriOptions) {
return requestPromise.post(request)
}
}
I get an error with my unit test code. (RequestError: Error: connect ECONNREFUSED 127.0.0.1:3000). How can I resolve it?
import { Client } from '../source/http/Client';
import { expect } from 'chai';
import requestPromise from 'request-promise-native';
import sinon from 'sinon';
describe('Client', () => {
afterEach(() => {
sinon.restore();
});
it('should pass', async () => {
const client = new Client();
const response = {};
const postStub = sinon.stub(requestPromise, 'post').resolves(mResponse);
const actual = await client.post({ uri: 'http://localhost:3000/api', method: 'POST' });
expect(actual).to.be.equal(response);
sinon.assert.calledWithExactly(postStub, { uri: 'http://localhost:3000/api', method: 'POST' });
});
});
What's incorrect in my code? I don't understand it.