Skip to content

Instantly share code, notes, and snippets.

@jan-molak
Created April 6, 2016 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jan-molak/c58c4c71413668df369489d6205ca6e0 to your computer and use it in GitHub Desktop.
Save jan-molak/c58c4c71413668df369489d6205ca6e0 to your computer and use it in GitHub Desktop.
Mocking JavaScript a little code examples
'use strict';
/** @constructor */
function Authoriser() { }
/**
* @public
*
* @param {String} username
* @param {String} password
*
* @returns {Boolean}
*/
Authoriser.prototype.authorise = function(username, password) {
// connects to LDAP or some database, verifies user credentials
// returns true if the username/password pair is valid, false otherwise
// ...
};
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);
});
});
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);
});
});
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);
});
});
var authoriser = Object.create(Authoriser.prototype),
mock = sinon.mock(authoriser);
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();
});
});
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
});
});
var authoriser = new Authoriser(),
system = new System(authoriser),
authorise_spy = sinon.spy(authoriser, 'authorise');
system.login('bob', 'SecretPassword');
expect(authorise_spy).to.have.been.called;
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;
});
});
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);
});
});
var rejecting_authoriser = { authorise: function() { return false; } };
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);
});
});
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);
});
});
'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