[英]RSpec how to mock controller method inside concern?
我有以下內容:
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
現在,我試圖在ObjectActions
模塊/關注對象內部模擬/添加“外部” error_notification
方法。
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
簡短的答案是
allow(self).to receive(:error_notification).and_return("Blah blah")
為什么?
您包括要在當前測試中測試的模塊
RSpec.describe ObjectActions, type: :concern do
include ObjectActions
這就是您應該嘲笑的內容。 但這是一種更好的方法,我不久前在此答案中對此進行了描述: https : //stackoverflow.com/a/48914463/299774
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.