Currently, I am testing a function that involves calling another function which returns a promise. The System Under Test (SUT) is structured as follows:
fn($modal) ->
modalInstance = $modal.open({
controller: 'myCtrl'
size: 'lg'
})
modalInstance.result.then(updateData)
For testing purposes, one approach could be to begin with the following setup:
it 'when modal called, results get updated with right data', ->
$modal = {
open: sinon.stub().returns({
result: $q.when([1, 2, 3])
})
}
fn($modal)
Subsequently, we can verify if the updatedData
matches [1, 2, 3].
However, it would also be beneficial to confirm whether $modal.open
has been invoked and if the appropriate parameters were provided. How can this be achieved?
In addition to stubbing the method, there is a need to spy on it. Should the entire $modal
object be mocked? Assistance is required regarding the correct syntax for this.
An attempt was made using the following code snippet:
mMk = sinon.mock($modal)
mMk.expects('open')
Nevertheless, Sinon raises an error message:
TypeError: Attempted to wrap open which is already stubbed