Skip to content

Instantly share code, notes, and snippets.

@killmenot
Created November 25, 2017 10:51
Show Gist options
  • Save killmenot/b579c0e7381481b23ba3278b744451a0 to your computer and use it in GitHub Desktop.
Save killmenot/b579c0e7381481b23ba3278b744451a0 to your computer and use it in GitHub Desktop.
How to correctly use Jasmine spies to mock transactions
function Account(statement, transaction){
this._statement = statement
this._transaction = transaction
this._balance = 0;
}
Account.prototype.deposit = function(amount) {
this._transaction.deposit(amount)
this._statement.storeHistory(amount, this._balance, "Deposit")
}
Account.prototype.withdraw = function(amount) {
this._transaction.withdraw(amount)
this._statement.storeHistory(amount, this._balance, "Withdraw")
}
Account.prototype.balance = function() {
return this._balance
}
module.exports = Account;
'use strict';
var Account = require('../../lib/account');
describe('Account', function () {
var account;
var statementSpy;
var transactionSpy;
beforeEach(function () {
statementSpy = jasmine.createSpyObj('statement', ['seeStatement', 'storeHistory']);
transactionSpy = jasmine.createSpyObj('transaction', ['balance', 'withdraw', 'deposit']);
account = new Account(statementSpy, transactionSpy);
});
it('is able for a user to deposit #1', function () {
account.deposit(40);
expect(transactionSpy.deposit).toHaveBeenCalledWith(40)
expect(statementSpy.storeHistory).toHaveBeenCalledWith(40, 0, "Deposit");
});
});
function Statement(){
this._logs = []
}
Statement.prototype.seeStatement = function() {
return this._logs
}
Statement.prototype.storeHistory = function(amount, balance, type) {
this._logs.push({amount: amount, balance: balance, type: type})
}
module.exports = Statement;
function Transaction(){
this._balance = 0
}
Transaction.prototype.balance = function() {
return this.balance
}
Transaction.prototype.deposit = function(amount) {
this._balance += amount
}
Transaction.prototype.withdraw = function(amount) {
this._balance -= amount
}
module.exports = Transaction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment