Saturday, 15 August 2015

angularjs - Testing Angular http interceptor -



angularjs - Testing Angular http interceptor -

i need create application log user in saved credentials , retry request on 401 , i'm having hard time testing because need initial phone call homecoming 401, homecoming 200 after login request happens again.

here test far:

it("should phone call login if status 401", function() { $httpbackend.whenget(api.baseurl + "/auth/login").respond(200); $httpbackend.whenget(api.baseurl + "/practice/active").respond(401); api.practiceactiveinquery(function(result) { expect(login.fn).tohavebeencalled(); }); $httpbackend.flush(); });

the problem need whenget of /practice/active respond 200 after it's called 1 time 401, or caught in circle. or thinking how test http interceptor wrong?

for this, want utilize expectget instead of whenget. expectget fulfills 1 request @ time, can alter response each time request made. this:

it("should phone call login if status 401", function() { $httpbackend.whenget(api.baseurl + "/auth/login").respond(200); $httpbackend.expectget(api.baseurl + "/practice/active").respond(401); //will homecoming 401 1st time $httpbackend.expectget(api.baseurl + "/practice/active").respond(200); //will homecoming 200 2nd time api.practiceactiveinquery(function(result) { expect(login.fn).tohavebeencalled(); }); $httpbackend.flush(); });

angularjs angularjs-http

No comments:

Post a Comment