unit testing - mocking a angularjs value to be reused within different tests -



have angularjs service makes use of angular value store username.

(function() {   'use strict';    angular.module('myapp.services', [])     .service('authservice', authservice)     .value('authstore', {       username: null     });    authservice.$inject = ['authstore'];    function authservice(authstore) {     var svc = this;     var _username = authstore.username;     svc.isloggedin = isloggedin;     svc.logout = logout;     svc.login = login;     svc.getusername = getusername;      function isloggedin() {       return _username != null && _username && _username != '';     }      function logout() {       authstore.username = null;       _username = null;     }      function getusername() {       return _username;     }      function login(username) {       authstore.username = username;       _username = username;     }   } })(); 

and i'm using jasmine try , test isloggedin method. tests be;

(function () {     'use strict'     describe('auth service tests', function () {         beforeeach(module('ionic'));         beforeeach(module('myapp.services'));         var provider         var authstore;         var authservice;          beforeeach(function () {             module(function ($provide) {                 authstore = {                     username: null                 };                 $provide.value('authstore', authstore);             });         });          beforeeach(inject(function (_authservice_) {             authservice = _authservice_;         }));          describe('isloggedin', function () {             it('should false username null.', function () {                 spyon(authstore, 'username').and.returnvalue(null);                 expect(authservice.isloggedin()).tobe(false); //success                 //(this because authstore initialized username: null, not because of spyon line above)                 console.log(authservice.getusername()); //null             });              it('should true username has value', function () {                 spyon(authstore, 'username').and.returnvalue('test');                 expect(authservice.isloggedin()).tobe(true);  //failed                 console.log(authservice.getusername()); //null             });         });     }); })(); 

the problem i'm having that. i'm unable change return value of username in authstore. returning mock value set in beforeeach.

1) missing here??

2) whats proper way mock angularjs value used in test. in scenario needs changed test test???

first, you're trying spy on field of type string (authstore.username). isn't possible. string not function. can't return anything. have value. spy used replace function another, fake one. not change value of string.

but isn't necessary anyway: since it's string, there nothing spy on. set value have. don't need provide fake implementation of it, since fake implementation identical real one:

(function () {     'use strict'     describe('auth service tests', function () {         beforeeach(module('ionic', 'myapp.services'));          var authstore;         var authservice;          beforeeach(inject(function (_authservice_, _authstore_) {             authservice = _authservice_;             authstore = _authstore_;         }));          describe('isloggedin', function () {             it('should false username null.', function () {                 authstore.username = null;                  expect(authservice.isloggedin()).tobe(false);                 console.log(authservice.getusername());             });              it('should true username has value', function () {                 authstore.username = 'test';                  expect(authservice.isloggedin()).tobe(true);                 console.log(authservice.getusername());             });         });     }); })(); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -