Rspec - 存根两个包含相同模块的 类 会抛出错误

Rspec - stubbing two classes that include the same module throws an error

我在使用 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 不喜欢我在同一个测试中包含相同模块的 类 存根。有什么办法解决这个问题,还是我做错了什么?

请尝试使用 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)