Another approach. This is pseudo-code similar to coffeescript. Ask in the comments if some expressions are not clear. I must say that this is not a direct answer to your question. Just a method of doing things like this differently.
Cleaner Tests
We introduce a variable that will be processed by the spy for broadcasting. I use pure spyware because spyOn can work poorly in some cumbersome cases when we are dealing with overriding or mixing a method.
rootScope$broadcastSpy = jasmine.createSpy()
We need to replace the original implementation with a stub that will handle spies and their states. The usual definition of stubs says that it is an entity that does not have its own logic. So, we do it in a clean way and do not put any logic here. Only the marker (which is represented by the spy).
beforeEach module ($provide) -> $provide.value '$rootScope', $broadcast: rootScope$broadcastSpy return
Of course, we need to talk with jasmine when we need to reset the spy.
beforeEach -> rootScope$broadcastSpy.reset()
Let me define a testing area where we will prepare all the necessary services and place them declaratively in context (i.e. this ).
instance = (fnAsserts) -> inject (loginService, $httpBackend, $timeout, apiUrl, AUTH_EVENTS) -> fnAsserts.call service: loginService apiUrl: apiUrl AUTH_EVENTS: AUTH_EVENTS '$httpBackend': $httpBackend '$timeout': $timeout
Record the test in Blackbox mode. We simply set the initial state, then start the action and at the end we give the opportunity to check our marker for changes.
it 'should make a POST request to API endpoint', instance ->
As you can see, we can associate calls with instances, add additional information to them and write each test in a cleaner way, because we see the state, scope, mocks and other necessary things in one place. We use closure only for the marker, and it ensures that side effects are minimized.