Created
November 25, 2017 10:51
-
-
Save killmenot/b579c0e7381481b23ba3278b744451a0 to your computer and use it in GitHub Desktop.
How to correctly use Jasmine spies to mock transactions
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
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; |
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'; | |
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"); | |
}); | |
}); |
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
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; |
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
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