【发布时间】: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