我可以使用什么RSpec方法来满足此要求

时间:2012-03-03 21:58:16

标签: ruby-on-rails unit-testing rspec mocking rspec-rails

背景:所以我粗略地(Ruby on Rails应用程序)

class A
   def calculate_input_datetimes
      # do stuff to calculate datetimes - then for each one identified
      process_datetimes(my_datetime_start, my_datetime_end)
   end

   def process_datetimes(input_datetime_start, input_datetime_end)
      # do stuff
   end
end

所以:

  • 我想测试一下calculate_input_datetimes算法是否有效 并计算传递给process_datetimes的正确日期时间
  • 我知道我可以删除process_datetimes,这样它的代码就不会了 参与测试

问题:我如何设置rspec测试,所以我可以具体 测试是否尝试将正确的日期时间传递给 process_datetimes,因此对于process_datetimes的给定规范测试 通过以下参数调用三(3)次说:

  • 2012-03-03T00:00:00 + 00:00,2012-03-09T23:59:59 + 00:00
  • 2012-03-10T00:00:00 + 00:00,2012-03-16T23:59:59 + 00:00
  • 2012-03-17T00:00:00 + 00:00,2012-03-23T23:59:59 + 00:00

感谢

1 个答案:

答案 0 :(得分:1)

听起来像你想要should_receive并使用with指定预期的参数,例如

a.should_receive(:process_datetimes).with(date1,date2)
a.should_receive(:process_datetimes).with(date3,date4)
a.calculate_input_datetimes

docs中有更多示例,例如,如果这些调用的顺序很重要,您可以使用.ordered

相关问题