2016-11-13 37 views
2

所以我有一个Weather类,它应该返回随机结果,并且我想使用stub来测试它。我在这http://www.martinfowler.com/articles/mocksArentStubs.html 阅读马丁花的文章,我觉得这将是最简单的解决方案。但很难找到任何语法的例子。你能给我一个测试的例子吗?这是我功课的一部分。在rspec中使用stub来测试rand的输出

class Weather 

    def conditions 
    return :good if chance > 0.3 
    :stormy 
    end 

    def chance 
    rand 
    end 

end 

回答

2

根据你的例子,你想测试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 
+0

很酷。很好,很简单 – Agata