Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test a private method that uses request, pipe and stream using mocks

I want to unit test the exported method in the code below. I want to mock the values in the private method to control the reject/resolves of the returned Promise. client is node-postgres object that is already connected to the database.

I know I can use proxyquire to stub out required libraries but how can I mock the chained methods .on('error', ...), .pipe(stream) and .on('end', ...) such that I can control the returned values.

Note the exported method shown is a simplification of the real one and exporting importDomain is not feasible.

const copyFrom = require('pg-copy-streams').from
const request = require('request')
const Promise = require('bluebird')

// private
function importDomain (client, domain) {
    return new Promise((resolve, reject) => {
    let stream = client.query(copyFrom(`COPY ${domain.table} FROM STDIN;`))

    let req = request(`${domain.url}`)
    req.on('error', reject)
    req.pipe(stream)
       .on('error', reject)
       .on('end', resolve)
  })
}

// public
module.exports = (client) => {
  let domain = someFunctionReturningDomain()
  importDomain(client, domain)
}
like image 831
Brian Avatar asked Mar 03 '17 21:03

Brian


People also ask

What is Mocking in unit testing Angular?

Introduction. Mocking is a great idea for testing Angular apps because it makes maintenance easier and helps reduce future bugs. There are a few complex tools, such as XUnit, for mocking an Angular CLI project. You can execute the mocking methods described in this guide only if you use vanilla Jasmine + Angular Testbed ...

How do you mock a service to inject in a unit test?

1 Answer. Show activity on this post. var service = new MockLoginService(); beforeEachProviders(() => [ provide(TestService, { useValue: service })]); it('should load languages', inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { tcb . createAsync(LoginComponent).

What is Spy in unit testing?

Spies. In a unit test, a test double is a replacement of a dependent component (collaborator) of the object under test. The test double does not have to behave exactly as the collaborator does. The purpose is to mimic the collaborator to make the object under test think that it is actually using the collaborator.

How do you mock Jasmine?

Using Jasmine spies to mock code Jasmine spies are easy to set up. You set the object and function you want to spy on, and that code won't be executed. In the code below, we have a MyApp module with a flag property and a setFlag() function exposed. We also have an instance of that module called myApp in the test.


1 Answers

In order to unit test the function importDomain, you will need to mock request. It's required by your module so should be considered to be tested and working normally.

Since you provide the client as a parameter. Its query method and the stream returned is already under full control out of the module.

Simply by replacing req.pipe, you will be able to control what is passed to the stream your client created.

like image 96
YLS Avatar answered Oct 11 '22 08:10

YLS