-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot test testdoubles inside .then function. #409
Comments
Yes, this is to be expected, because even though |
What would be the best alternative for testing whether next() is called is this scenario. |
Here is a way to make what you're doing work const td = require('testdouble');
describe('Mock function testing', () => {
it(' unable to check next function is invoked even once', (done) => { // <- done callback
const mockFunc = td.function('mockFunc');
td.when(mockFunc()).thenResolve('abcdef');
const next = td.function();
const validateCustomer = () => {
return (next) => {
return mockFunc().then((response) => { //<-- return a promise
return next();
});
}
}
validateCustomer()(next).then(() => { // <-- chain off that promise
td.verify(mockFunc(), {times:1})
td.verify(next(), {times: 1})
done() // <-- signal to your test runner that it's done so it waits
});
})
}) However, a (IMO) better code design is almost always to return values and assert those values than verify something was called, because the latter is specifying a side-effect, which (as you have seen) are harder to observe than simply returning something, even via a promise. Something like this: const td = require('testdouble');
const assert = require('assert');
describe('Mock function testing', () => {
it(' unable to check next function is invoked even once', (done) => {
const mockFunc = td.function('mockFunc');
td.when(mockFunc()).thenResolve('abcdef');
const next = td.when(td.func('abcdef')).thenResolve('foo');
const validateCustomer = () => {
return (next) => {
return mockFunc().then((response) => {
return next(response);
});
}
}
validateCustomer()(next).then((result) => {
assert.equal(result, 'foo')
done()
});
})
}) These examples weren't runnable so I just edited them inline, but in broad strokes they should work. |
Description
I am trying to work on a closure where I pass a testdouble(const next = td.func()) to inner function.
Inside inner function I create a testdouble which returns a promise.
when handling the promise i.e., inside .then block I call the testdouble that I passed to inner function.
Issue
When I try to check the testdouble the pass down to innerfunction and invoked inside .then block it says I din't invoke the testdouble.
Environment
node -v
output: 10.5.0npm -v
(oryarn --version
) output: 6.1.0npm ls testdouble
(oryarn list testdouble
) version: 3.11.0Code-fenced Examples
The text was updated successfully, but these errors were encountered: