【问题标题】:Using stub in rspec to test output of a rand在 rspec 中使用存根来测试 rand 的输出
【发布时间】:2017-03-27 13:28:21
【问题描述】:

我有一个天气类,它应该返回随机结果,我想使用method stub 对其进行测试。我在page 上阅读了 Martin Flower 写的文章,我觉得这将是最简单的解决方案。但是很难找到任何语法示例。

你能给我一个测试的例子吗?这是我作业的一部分。

class Weather

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

  def chance
   rand
  end

end

【问题讨论】:

    标签: ruby rspec stub


    【解决方案1】:

    根据您的示例,您想测试 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
    

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      相关资源
      最近更新 更多