在rspec中测试变量赋值

时间:2014-04-21 05:15:32

标签: ruby rspec

有谁知道为什么这个测试失败了?我试图做的就是证明这种方法(起始位置)将current_room分配给starting_room,而我无法获得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

我的输出:

 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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您指定@starting_room并且不指定current_room。您需要触发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