Skip to content

Instantly share code, notes, and snippets.

@jameslafa
Created June 11, 2014 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameslafa/f4a915331e376dd0981f to your computer and use it in GitHub Desktop.
Save jameslafa/f4a915331e376dd0981f to your computer and use it in GitHub Desktop.
AngularJS: serve different responses with $httpBackend inside the same test (auto retry for example)
# More details on https://blog.james-lafa.fr/angularjs-how-to-test-a-retry-after-http-error-with-httpbackend/
sessionCalls = 0
$httpBackend.when('POST', '/sessions').respond( (method, url, data, headers) ->
if sessionCalls == 0
sessionCalls++
return [200, {"response": 1}, {}]
else
sessionCalls++
return [200, {"response":0,"auth_token":"HK7i6DRncWdBmq"}, {}]
)
Backend.createSession()
$httpBackend.flush()
expect(sessionCalls).toEqual(2)
expect(Backend.isLoggedIn()).toBe(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment