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)
}
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 ...
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With