我有以下内容:
class PaymentController < ActionController::API
include ObjectActions
def error_notification(message)
puts "An error has occurred: #{message}"
end
end
module ObjectActions
extend ActiveSupport::Concern
def process
if valid?
# process payment
else
error_notification("Payment is not valid")
end
end
end
现在,我试图模仿/存储&#34;外部&#34; error_notification
模块/关注中的ObjectActions
方法。
RSpec.describe ObjectActions, type: :concern do
include ObjectActions
before do
allow(described_class).to receive(:valid?).and_return(false)
# I KNOW THIS IS NOT RIGHT, HOW CAN I PROPERLY MOCK IT?
allow(described_class).to receive(:error_notification).and_return("Blah blah")
end
context '#process' do
it { expect { process }.to eq("Blah blah") }
end
end
答案 0 :(得分:1)
简短回答是
allow(self).to receive(:error_notification).and_return("Blah blah")
为什么?
您要包含该模块,您想在当前测试中进行测试
RSpec.describe ObjectActions, type: :concern do
include ObjectActions
所以这就是你应该嘲笑的。但这是一个更好的方法,我在不久前在这个答案中描述了这一点:https://stackoverflow.com/a/48914463/299774