Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec expect throws "undefined method"

Tags:

ruby

rspec

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.

like image 333
khebbie Avatar asked Sep 24 '26 05:09

khebbie


2 Answers

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
like image 182
Jason Noble Avatar answered Sep 25 '26 19:09

Jason Noble


i think it should be

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

like image 33
Jed Schneider Avatar answered Sep 25 '26 20:09

Jed Schneider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!