I have got this working now after chaining my callback timeout function to not call itself but to call another referenced function, similar to this:
http://jsfiddle.net/robinroestenburg/aDwva/My Jasmine tests looks like this: (CoffeeScript)
it("Should deal with sucsessful Agent Stats Lookup", inject((MyService) =>
# Check Service created
expect(MyService).toBeDefined()
# Expected Assessor Stats
expectedStats = {stats: {data:"123"} }
# Expected Call To HTTP Service
@httpMock.when('GET','/secure/assessment/stats.form').respond(expectedStats)
# Run the test!
MyService.loadStats()
# Force the polling function through timeout
@timeoutMock.flush()
# Flush the expected HTTP request
@httpMock.flush()
# Expected Broadcast to fire with contents of stats
expect(@rootScope.$broadcast).toHaveBeenCalledWith('onDownloaded', { stats: expectedStats } );
# Check logs contain expected values
expect(@logMock.info.logs[0]).toContain("Downloading Stats")
expect(@logMock.error.logs.length).toEqual(0)
))
My final questions regarding this is why the test still passes even if I don't have the expected call from my spied $broadcast, it fails if I pass invalid args to the expected but if I don't have it at all I thought it should fail?
expect(@rootScope.$broadcast).toHaveBeenCalledWith('onDownloaded', { stats: expectedStats } );
James