tags:

views:

33

answers:

2

I am a complete Ruby newby and am playing around with rspec

I am testing a class (Account) that has this line:

attr_reader :balance

When I try to test it with this method:

it "should deposit twice" do
  @acc.deposit(75)
  expect {
    @acc.deposit(50)
    }.to change(Account.balance).to(125)
end

I get this error:

NoMethodError in 'Account should deposit twice'
undefined method `balance' for Account:Class

I don't understand why I get the error since the attribute 'balance' exists, however I can see that it is not a method, but shouldn't rspec be able to find it anyway?

Update: As Jason noted I should be @acc.balance, since this is what I am asserting. But I get 'nil is not a symbol' when doing this.

+3  A: 

It should be @acc.balance

it "should deposit twice" do
  @acc = Account.new
  @acc.deposit(75)
  @acc.balance.should == 75
  expect {
    @acc.deposit(50)
    }.to change(@acc, :balance).to(125)
end
Jason Noble
The attr_reader is on an instance of the Account class, not the class itself.
Jason Noble
Changed that (it was the way I got it originally) and now I get 'nil is not a symbol'
khebbie
I edited the code and this is working for me now.
Jason Noble
A: 

i think it should be

expect {@acc.deposit(50)}.to change(@acc.balance}.to(125)

Jed Schneider