I am trying to use mockService in my sinon-chai tests. It looked really helpful when I read the README file. However, I ran into several problems trying to use it, and even now that I solved the problem, I don't fully understand the function's behaviour nor effect, yet. Using my example:
I have a service that injects UI-router's $state service. Since I am not really interested in having to deal with the effects of $state or any inconveniences, I thought in giving mockService a go. This was my beforeEach block on the first try:
I left the mockService with just one argument because I thought it would make every function return void and everything would be alright. When I ran the tests, though, I got the following error:
TypeError: 'undefined' is not an object (evaluating 'config._default')
This was in the exact line of mockService. In order to solve this I used an empty object as the second argument, like so
bard.mockServices($state, {});
based on the assumption that the function isn't prepared for not receiving the second argument. With this code, when I ran the tests, I got rid of the first error and got this one instead:
TypeError: 'undefined' is not a function (evaluating '$state.go('/phase/step', {phaseId:phaseNumber})')
I thought it was related to the $state.go function not being defined, not even as a stub or spy... I tried again, now with
bard.mockService($state, {
go : sinon.stub()
});
in order to explicitly tell the function that I wanted an empty stub for $state.go (which, BTW, is the only function of $state I am currently using). This got rid of the errors!
Now, my question actually is... Why did those 2 errors happened in the first place?
Also, I recommend some more explanation for this function in the README, since it is one of the most useful of the provided. I checked the docs in the source and they are much more complete, you should try to make something more like it.
Anyway, great project, I don't even remember how to inject dependencies without this, anymore.
UPDATE
My workaround, though working, could lead to some errors. The other day I've noticed that what it was doing, actually, was telling that the stubbed function should return a sinon.stub(). I guess I forgot how the function worked.
So, a more correct workaround is to declare an appropriate return value for the functions to be mocked, even an undefined is enough. By doing exactly what I was doing, you could eventually get into trouble if ever using the return value of the mocked function.
I am trying to use
mockService
in my sinon-chai tests. It looked really helpful when I read the README file. However, I ran into several problems trying to use it, and even now that I solved the problem, I don't fully understand the function's behaviour nor effect, yet. Using my example:I have a service that injects UI-router's
$state
service. Since I am not really interested in having to deal with the effects of$state
or any inconveniences, I thought in givingmockService
a go. This was mybeforeEach
block on the first try:I left the mockService with just one argument because I thought it would make every function return void and everything would be alright. When I ran the tests, though, I got the following error:
TypeError: 'undefined' is not an object (evaluating 'config._default')
This was in the exact line of
mockService
. In order to solve this I used an empty object as the second argument, like sobard.mockServices($state, {});
based on the assumption that the function isn't prepared for not receiving the second argument. With this code, when I ran the tests, I got rid of the first error and got this one instead:
TypeError: 'undefined' is not a function (evaluating '$state.go('/phase/step', {phaseId:phaseNumber})')
I thought it was related to the
$state.go
function not being defined, not even as a stub or spy... I tried again, now within order to explicitly tell the function that I wanted an empty stub for
$state.go
(which, BTW, is the only function of$state
I am currently using). This got rid of the errors!Now, my question actually is... Why did those 2 errors happened in the first place?
Also, I recommend some more explanation for this function in the README, since it is one of the most useful of the provided. I checked the docs in the source and they are much more complete, you should try to make something more like it.
Anyway, great project, I don't even remember how to inject dependencies without this, anymore.
UPDATE
My workaround, though working, could lead to some errors. The other day I've noticed that what it was doing, actually, was telling that the stubbed function should return a
sinon.stub()
. I guess I forgot how the function worked.So, a more correct workaround is to declare an appropriate return value for the functions to be mocked, even an
undefined
is enough. By doing exactly what I was doing, you could eventually get into trouble if ever using the return value of the mocked function.