Skip to content

Commit

Permalink
add memoized subject
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Oct 10, 2009
1 parent 76eecef commit 5a00204
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
Binary file modified RSpecTipsAndTricks.key
Binary file not shown.
2 changes: 1 addition & 1 deletion code/implicit_subject/account_spec.rb
Expand Up @@ -8,6 +8,6 @@
end

context "when first created" do
it { should have_a_balance_of(Money.new(0, :BLR))}
it { should have_a_balance_of(0, :BLR)}
end
end
6 changes: 5 additions & 1 deletion code/lib/account.rb
Expand Up @@ -5,11 +5,15 @@ class Account

def initialize(amount=nil, currency=nil)
if amount && currency
@balance = Money.new(amount, currency)
deposit(amount, currency)
else
@balance = Money.new(0, :BLR)
end
end

def deposit(amount, currency)
@balance = Money.new(amount, currency)
end

def inspect
"Account (#{balance.inspect})"
Expand Down
10 changes: 10 additions & 0 deletions code/memoized_subject/account_spec.rb
@@ -0,0 +1,10 @@
require 'spec_helper'

describe Account do
context "with a zero balance" do
it "accepts deposits" do
subject.deposit(12, :USD)
subject.should have_a_balance_of(12, :USD)
end
end
end
4 changes: 2 additions & 2 deletions code/spec/support/matchers.rb
@@ -1,6 +1,6 @@
Spec::Matchers.define :have_a_balance_of do |money|
Spec::Matchers.define :have_a_balance_of do |amount, currency|
match do |account|
account.balance == money
account.balance == Money.new(amount, currency)
end
end

0 comments on commit 5a00204

Please sign in to comment.