【发布时间】:2010-12-22 02:25:53
【问题描述】:
我想用 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 上调用-?
【问题讨论】:
标签: ruby unit-testing rspec rspec2