Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 在rspec中测试变量赋值_Ruby_Rspec - Fatal编程技术网

Ruby 在rspec中测试变量赋值

Ruby 在rspec中测试变量赋值,ruby,rspec,Ruby,Rspec,有人知道为什么这次考试不及格吗?我要做的就是证明这个方法(起始位置)将当前房间分配给起始房间,而我不能得到当前房间的返回值 class Room attr_reader :starting_room, :current_room def initialize @starting_room = "Room 5" @current_room = nil end def starting_positions @current_room = startin

有人知道为什么这次考试不及格吗?我要做的就是证明这个方法(起始位置)将当前房间分配给起始房间,而我不能得到当前房间的返回值

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

有什么想法吗?

你分配了
@开始的房间
,而没有分配
当前的房间
。您需要触发器
开始位置

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

似乎
@room
为零。请添加
p@room.class
。那么我在课堂上没有看到读者,他们在哪里?啊,对不起,有读者,我只是没有把他们抄过来(现在就这么做…),那么
@room
分配给它的作业在哪里?嗯,我不太清楚你的意思。您是指使用before(:each)创建的新@room对象吗?如果有效,答案是肯定的