我有一个简单的方法,我想用 RSpec 进行测试。我想确保apply
减player.capacity
一。为此,我模拟了一个播放器对象并正在测试它是否接收到正确的消息。
代码
class DecreaseCapacity < Item
def apply player
player.capacity -= 1
end
end
测试
describe DecreaseCapacity, "#apply" do
it "should decrease capacity by one" do
player = double()
player.should_receive(:capacity) # reads the capacity
player.should_receive(:capacity=) # decrement by one
subject.apply player
end
end
失败信息
1) DecreaseCapacity#apply should decrease the player's capacity by one
Failure/Error: subject.apply player
undefined method `-' for nil:NilClass
# ./item.rb:39:in `apply'
# ./item_spec.rb:25
这里发生了什么?为什么要player.capacity -= 1
尝试-
调用nil
?