【问题标题】:Rspec - stubbing two classes that include the same module throws an errorRspec - 存根包含相同模块的两个类会引发错误
【发布时间】:2021-11-14 22:51:40
【问题描述】:

我在使用 Rspec 时遇到了一些奇怪的存根问题。我有两个功能相似的类,它们都包含一个模块:

class Transaction
  module Overview
  ...
  end
end

class Actual
  class Overview
    include Transaction::Overview
  end
end

class Adjustment
  class Overview
    include Transaction::Overview
  end
end

我在一个方法中使用它们,并试图在这样的测试中存根它们:

actual_quarters = Actual::Overview::AllQuarters.new(actuals)
actual_overview = double("Actual::Overview", all_quarters: actual_quarters, value_for_report_quarter: 0)
expect(Actual::Overview).to receive(:new).with(activity_presenter, report_presenter).at_least(:once).and_return(actual_overview)

adjustment_quarters = Adjustment::Overview::AllQuarters.new(adjustments)
adjustment_overview = double("Adjustment::Overview", all_quarters: adjustment_quarters, value_for_report_quarter: 0)
expect(Adjustment::Overview).to receive(:new).with(activity_presenter, report_presenter).at_least(:once).and_return(adjustment_overview)

但是,运行测试给了我错误:

  1) Report::Export Report::Export::Row includes the actuals for the previous quarters
     Failure/Error: expect(Adjustment::Overview).to receive(:new).with(activity_presenter, report_presenter).at_least(:once).and_return(adjustment_overview)
       Transaction::Overview does not implement: new

如果我像这样颠倒存根的顺序:

adjustment_quarters = Adjustment::Overview::AllQuarters.new(adjustments)
adjustment_overview = double("Adjustment::Overview", all_quarters: adjustment_quarters, value_for_report_quarter: 0)
expect(Adjustment::Overview).to receive(:new).with(activity_presenter, report_presenter).at_least(:once).and_return(adjustment_overview)

actual_quarters = Actual::Overview::AllQuarters.new(actuals)
actual_overview = double("Actual::Overview", all_quarters: actual_quarters, value_for_report_quarter: 0)
expect(Actual::Overview).to receive(:new).with(activity_presenter, report_presenter).at_least(:once).and_return(actual_overview)

我收到此错误:

  1) Report::Export Report::Export::Row includes the actuals for the previous quarters
     Failure/Error: expect(Actual::Overview).to receive(:new).with(activity_presenter, report_presenter).at_least(:once).and_return(actual_overview)
       Transaction::Overview does not implement: new

这表明 Rspec 不喜欢我在同一个测试中包含相同模块的存根类。有什么办法可以解决这个问题,还是我做错了什么?

【问题讨论】:

    标签: ruby-on-rails rspec rspec-rails stubbing


    【解决方案1】:

    尝试改用instance_double。在没有看到您的代码和测试设置的情况下,尝试将您的测试代码更改为:

    # is this the class you are testing? if so, use described_class.new(actuals) instead
    actual_quarters = Actual::Overview::AllQuarters.new(actuals) 
    
    actual_overview_double = instance_double(Actual::Overview, all_quarters: actual_quarters)
    
    # I don't think this should be at_least(:once) - if it is being instantiated more than once, you may need another instance_double
    expect(Actual::Overview).to receive(:new).with(activity_presenter, report_presenter).and_return(actual_overview_double)
    
    # again - should this be described_class?
    adjustment_quarters = Adjustment::Overview::AllQuarters.new(adjustments)
    
    adjustment_overview_double = instance_double(Adjustment::Overview, all_quarters: adjustment_quarters, value_for_report_quarter: 0)
    
    # again about the at_least(:once)
    expect(Adjustment::Overview).to receive(:new).with(activity_presenter, report_presenter).and_return(adjustment_overview)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 2014-08-16
      • 1970-01-01
      • 2012-10-24
      • 2021-12-18
      • 2013-05-23
      相关资源
      最近更新 更多