I have a async function I'd like to monitor if it's being called and what the arguments are. The function takes in two arguments makes a network request and comes back with a response based on the two arguments, for instance let's say it's an addition api. You pass in 1 and 1 and get 2. Rather then making this request I'd like to create a mock for it where I replace this method with it's syncronously counterpart. For instance.
Here's the original add function:
const async realAdd = (a, b) => {
return makeRequest(`http://ift.tt/2wCYs6t}`)
}
Here's the one I'd like to use for mocking:
const async dummyAdd = (a, b) => {
return Promise.resolve(a + b)
}
Let's say there is a function that takes add and uses it. In this case this is function I'd like to use, and I'd like to mock add with the previous version.
const mockAdd = sinon.something(dummyAdd)
let result = someFunctionThatUses(mockAdd)
assert.equal(mockAdd.called, 1)
All I want is a way to pass in the dummy add method and check if it's been called, but I still want the add function to return the promise.
Aucun commentaire:
Enregistrer un commentaire