Created
April 6, 2016 15:52
-
-
Save jan-molak/c58c4c71413668df369489d6205ca6e0 to your computer and use it in GitHub Desktop.
Mocking JavaScript a little code examples
This file contains 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('A newly created System', function() { | |
var dummy_authoriser = { }; | |
it('has no logged in users', function() { | |
var system = new System(dummy_authoriser); | |
expect(system.loginCount()).to.equal(0); | |
}); | |
}); |
This file contains 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('A System', function () { | |
var fake_authoriser = sinon.createStubInstance(Authoriser); | |
fake_authoriser.authorise | |
.returns(false) | |
.withArgs('bob', 'SecretPassword').returns(true), | |
system = new System(fake_authoriser); | |
it('allows Bob in', function () { | |
system.login('bob', 'SecretPassword'); | |
expect(system.loginCount()).to.equal(1); | |
}); | |
it('does not allow Alice in', function () { | |
system.login('alice', 'password'); | |
expect(system.loginCount()).to.equal(0); | |
}); | |
}); |
This file contains 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('A System', function() { | |
var fake_authoriser = { | |
authorise: function(username, password) { | |
return (username === 'bob' && password === 'SecretPassword'); | |
} | |
}, | |
system = new System(fake_authoriser); | |
it('allows Bob in', function() { | |
system.login('bob', 'SecretPassword'); | |
expect(system.loginCount()).to.equal(1); | |
}); | |
it('does not allow Alice in', function() { | |
system.login('alice', 'password'); | |
expect(system.loginCount()).to.equal(0); | |
}); | |
}); |
This file contains 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
var authoriser = Object.create(Authoriser.prototype), | |
mock = sinon.mock(authoriser); |
This file contains 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('A System', function() { | |
var authoriser = new Authoriser(), | |
mock = sinon.mock(authoriser); | |
afterEach(function() { | |
mock.restore(); | |
}); | |
it('delegates logging users in to the authoriser', function() { | |
// prepare | |
var system = new System(authoriser); | |
// expect | |
mock.expects('authorise').once().returns(true); | |
// act | |
system.login('bob', 'SecretPassword'); | |
// assert | |
mock.verify(); | |
}); | |
}); |
This file contains 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('A System', function() { | |
var accepting_authoriser_mock = { | |
authorise_was_called: false, | |
authorise: function() { | |
this.authorise_was_called = true; | |
return true; | |
}, | |
verify: function() { | |
expect(this.authorise_was_called).to.equal(true); | |
} | |
}; | |
it('delegates logging users in to the authoriser', function() { | |
var system = new System(accepting_authoriser_mock); | |
system.login('bob', 'SecretPassword'); | |
accepting_authoriser_mock.verify(); // assertions moved to #verify | |
}); | |
}); |
This file contains 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('A System', function() { | |
var accepting_authoriser = sinon.createStubInstance(Authoriser); | |
accepting_authoriser.authorise.returns(true); | |
afterEach(function() { | |
accepting_authoriser.authorise.restore(); | |
}); | |
it('delegates logging users in to the authoriser', function() { | |
var system = new System(accepting_authoriser); | |
system.login('bob', 'SecretPassword'); | |
expect(accepting_authoriser.authorise).to.have.been.called; | |
}); | |
}); |
This file contains 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('A System', function() { | |
var accepting_authoriser_spy = { | |
authorise_was_called: false, | |
authorise: function() { | |
this.authorise_was_called = true; | |
return true; | |
} | |
} | |
afterEach(function() { | |
accepting_authoriser_spy.authorise_was_called = false | |
}; | |
it('delegates logging users in to the authoriser', function() { | |
var system = new System(accepting_authoriser_spy); | |
system.login('bob', 'SecretPassword'); | |
expect(accepting_authoriser_spy.authorise_was_called).to.equal(true); | |
}); | |
}); |
This file contains 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('A System with a logged in user', function() { | |
var accepting_authoriser = sinon.createStubInstance(Authoriser); | |
accepting_authoriser.authorise.returns(true); | |
it('has a loginCount of 1', function() { | |
var system = new System(accepting_authoriser); | |
system.login('bob', 'SecretPassword'); | |
expect(system.loginCount()).to.equal(1); | |
}); | |
}); |
This file contains 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('A System with a logged in user', function() { | |
var accepting_authoriser = { authorise: function() { return true; } }; | |
it('has a loginCount of 1', function() { | |
var system = new System(accepting_authoriser); | |
system.login('bob', 'SecretPassword'); | |
expect(system.loginCount()).to.equal(1); | |
}); | |
}); |
This file contains 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
'use strict'; | |
/** | |
* @constructor | |
* @param {Authoriser} authoriser | |
*/ | |
function System(authoriser) { | |
/** @private */ | |
var login_count = 0; | |
/** @public */ | |
this.login = function(username, password) { | |
if (authoriser.authorise(username, password)) { | |
++login_count; | |
} | |
}; | |
/** @public */ | |
this.loginCount: function() { | |
return login_count; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment