【问题标题】:what RSpec approach could I use for this requirement我可以使用什么 RSpec 方法来满足这个要求
【发布时间】:2012-03-03 21:58:15
【问题描述】:

背景:所以我大致有(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
  • 我知道我可以 STUB 出 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

谢谢

【问题讨论】:

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


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多