Skip to content

Instantly share code, notes, and snippets.

@srph
Created September 12, 2014 08:24
Show Gist options
  • Select an option

  • Save srph/0121910e7e04d84f3840 to your computer and use it in GitHub Desktop.

Select an option

Save srph/0121910e7e04d84f3840 to your computer and use it in GitHub Desktop.
$httpBackend DRY - Jasmine
describe('Authentication Service', function() {
beforeEach(module('app'));
beforeEach(module('stateMock'));
var $httpBackend, $state, srvc;
var checkHandler;
var guestHandler;
var dataHandler;
var loginHandler;
var data = {
username: 'blabla',
password: 'blabla'
};
beforeEach(inject(function(_$httpBackend_, AuthSrvc, _$state_) {
$httpBackend = _$httpBackend_;
srvc = AuthSrvc;
$state = _$state_;
$state.expectTransitionTo('main');
checkHandler = $httpBackend.whenGET('/api/auth/check');
guestHandler = $httpBackend.whenGET('/api/auth/guest');
// dataHandler = $httpBackend.whenGET('/api/auth/data');
// loginHandler = $httpBackend.whenGET('/api/auth/login');
}));
afterEach (function () {
$httpBackend.verifyNoOutstandingExpectation ();
$httpBackend.verifyNoOutstandingRequest ();
});
describe("Query", function() {
it("should request to server if session is authenticated", function () {
checkHandler.respond(200, { status: true });
$httpBackend.expectGET('/api/auth/check');
srvc.query.check();
$httpBackend.flush();
// expect(srvc.status).toBeTruthy();
// promise.then(function () {
// // expect(srvc.status).toBe(true);
// });
// expect(srvc.status).toEqual(true);
});
it("should request to server if session is guest", function () {
guestHandler.respond({ status: false });
$httpBackend.expectGET('/api/auth/guest')
srvc.query.guest();
$httpBackend.flush();
// expect(srvc.status).toBe(false);
});
it("should fetch auth data", function () {
$httpBackend.expectGET('/api/auth/data')
.respond(200, {
status: true
});
srvc.query.data();
$httpBackend.flush();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment