Last active
August 29, 2015 14:17
-
-
Save avanslaars/a7df470ec6219bfb742b to your computer and use it in GitHub Desktop.
Simple example of unit testing run block in angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('run block', function(){ | |
var sandbox, | |
MockCookieStore, | |
MockAuthService, | |
token | |
beforeEach(function(){ | |
sandbox = sinon.sandbox.create() | |
MockCookieStore = sandbox.stub({get:function(key){}}) | |
MockAuthService = sandbox.stub({LoadToken:function(uObj){}}) | |
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlfQ.eoaDVGTClRdfxUZXiPs3f8FmJDkDE_VCQFXqKxpLsts' | |
module('MyApplication',function($provide){ | |
$provide.value('$cookieStore',MockCookieStore) | |
$provide.value('AuthService',MockAuthService) | |
}) | |
}) | |
afterEach(function(){ | |
sandbox.restore() | |
}) | |
it('should check for a token cookie and load it if found', function(done){ | |
MockCookieStore.get.returns(token) | |
inject() //THIS IS THE KEY! | |
expect(MockCookieStore.get).to.have.been.calledWith('auth-token') | |
expect(MockAuthService.LoadToken).to.have.been.calledWith(token) | |
done() | |
}) | |
it('should not try to load a token when one is not found', function(done){ | |
MockCookieStore.get.returns(undefined) | |
inject() //THIS IS THE KEY! | |
expect(MockCookieStore.get).to.have.been.calledWith('auth-token') | |
expect(MockAuthService.LoadToken).not.to.have.been.called | |
done() | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
angular.module('MyApplication') | |
.run(appStartup) | |
appStartup.$inject = ['$cookieStore', 'AuthService'] | |
function appStartup($cookieStore, AuthService) | |
{ | |
var token = $cookieStore.get('auth-token') | |
if(token){ | |
AuthService.LoadToken(token) | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment