使用 rspec 中的存根来测试随机数的输出
Using stub in rspec to test output of a rand
我有一个 class 天气,它应该是 return 随机结果,我想使用 method stub. I read the article written by Martin Flower on this page 对其进行测试,我认为这将是最简单的解决方案。但是很难找到任何语法示例。
你能给我一个测试的例子吗?这是我作业的一部分。
class Weather
def conditions
return :good if chance > 0.3
:stormy
end
def chance
rand
end
end
根据您的示例,您想测试 chance
的行为而不是实现。
describe Weather do
it 'returns good' do
weather = Weather.new
allow(weather).to receive(:chance).and_return(0.8)
expect(weather.conditions).to eq :good
end
end
我有一个 class 天气,它应该是 return 随机结果,我想使用 method stub. I read the article written by Martin Flower on this page 对其进行测试,我认为这将是最简单的解决方案。但是很难找到任何语法示例。
你能给我一个测试的例子吗?这是我作业的一部分。
class Weather
def conditions
return :good if chance > 0.3
:stormy
end
def chance
rand
end
end
根据您的示例,您想测试 chance
的行为而不是实现。
describe Weather do
it 'returns good' do
weather = Weather.new
allow(weather).to receive(:chance).and_return(0.8)
expect(weather.conditions).to eq :good
end
end