0

Does anyone know why this test is failing? All I'm trying to do is prove this method (starting positions) assigns the current_room to the starting_room and I can't get a value returned for current_room?

class Room
  attr_reader :starting_room, :current_room  

  def initialize
    @starting_room = "Room 5"
    @current_room = nil
  end

  def starting_positions
    @current_room = starting_room
  end

end


 before(:each) do
   @room = Room.new
 end

 describe '#starting_positions' do
  it 'sets the starting location to the current location' do
    @room.instance_variable_set(:@starting_room, "Room 5")
    expect(@room.current_room).to eql("Room 5")
  end
end

My output:

 Failures:

 1) Room#starting_positions sets the starting location to the current location
 Failure/Error: expect(@room.current_room).to eql("Room 5")

   expected: "Room 5"
        got: nil

Any ideas?

4
  • It seems @room is nil. please add p @room.class. Then I don't see the readers in the class, where are they? Commented Apr 21, 2014 at 5:27
  • Ahhh sorry, there are readers, I just didn't copy them over (doing that now...) Commented Apr 21, 2014 at 5:34
  • so then @room where is the assignment to it? Commented Apr 21, 2014 at 5:36
  • hmmm, I'm not too sure what you mean. Are you referring to the new @room object that is created with before(:each)? Commented Apr 21, 2014 at 5:41

1 Answer 1

2

You assign @starting_room and don't assign current_room. You need trigger starting_positions

before(:each) do
  @room = Room.new
end

describe '#starting_positions' do
  it 'sets the starting location to the current location' do
    @room.instance_variable_set(:@starting_room, "Room 5")
    @room.starting_positions
    expect(@room.current_room).to eql("Room 5")
  end
end
Sign up to request clarification or add additional context in comments.

Comments

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.