vendredi 20 février 2015

Jasmine, AngularJS change injected mock object after injection

I want to test my factory in different situations such as the absence of a token, an expired token and a good token. I am having trouble with mocking the $window.localStorage. Here is my test code:



describe('Unit: Security Module', function () {

describe('Security factory', function() {
var windowMock, locationMock, security, $httpBackend, $http, $window, $location;

beforeEach(module('app.common.security', function($provide) {
locationMock = {
url: jasmine.createSpy().and.returnValue(true)
};
windowMock = {
localStorage: { token: false }
};
$provide.value('$location', locationMock);
$provide.value('$window', windowMock);
}));

beforeEach(inject(function(_$httpBackend_, _$window_, _$location_, _security_) {
$httpBackend = _$httpBackend_;
$window = _$window_;
$location = _$location_;
security = _security_;
}));

it("Should call location.url with '/login' if no token in localStorage", function() {
security.tokenCheck();
expect($location.url).toHaveBeenCalledWith('/login');
});

it("Should call location.url with '/login' and delete token if bad token", function() {
// Need to modify the windowMock.localStorage.token value here somehow

$httpBackend
.whenPOST('/api/tokenCheck', {token:"mockBadToken123"})
.respond (400, "Token invalid or undefined");

security.tokenCheck();
expect($location.url).toHaveBeenCalledWith('/login');
expect($window.localStorage.token).toBeUndefined();
});

});

});


My question is: if I want to modify the windowMock values, do I have to write all my tests like this?



it("good token", function () {

module('app.common.security', function ($provide) {
locationMock = {
url: jasmine.createSpy().and.returnValue(true)
};
windowMock = {
localStorage: { token: "mockBadToken123" }
};
$provide.value('$location', locationMock);
$provide.value('$window', windowMock);
});

inject(function (_$httpBackend_, _$window_, _$location_, _security_) {
$httpBackend = _$httpBackend_;
$window = _$window_;
$location = _$location_;
security = _security_;
})

$httpBackend
.whenPOST('/api/tokenCheck', {token: "mockBadToken123"})
.respond(400, "Token invalid or undefined");

security.tokenCheck();
expect($location.url).toHaveBeenCalledWith('/login');
expect($window.localStorage.token).toBeUndefined();


});


Or is there a more efficient way?


Aucun commentaire:

Enregistrer un commentaire