1

I don't have any code as this is just a hypothetical question, but how would you go about accessing and manipulating instance variables in rspec?

For instance if you had a @counter variable in your initialize method inside of a class, how could you write a test saying that if @counter is a certain number, then a certain method should return true. And if it equals a different number, then that method should return false.

1 Answer 1

1
describe Foo do
  context 'When counter is even' do
    let( :foo ){ Foo.new(4) }

    specify '#even?' do
      expect( foo.even? ).to be_true
    end
  end
end

According to your question, @counter is set in the initialize method. So the #even? method would check @counter.

Sign up to request clarification or add additional context in comments.

4 Comments

To clarify, the counter would not be set like that. A players name would be the argument in the initialize definition and the counter is automatically set to 0 when you initialize. After a player takes a turn, the counter increments by one each time
It sounds like you want a #has_moved? method. So that method would check the @counter. The above test will work.
Okay cool. I'll try and mess around with some code. Thanks for your help!
Writing specs is a great way to learn. And then you can post your app and spec code in questions on SO to get better help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.