我有一个服务,我想测试是否调用了一个函数。我不知道如何测试它,因为它似乎没有一个subject
正在被操作。
class HubspotFormSubmissionService
def initialize(form_data)
@form_data = form_data
end
def call
potential_client = createPotentialClient
end
def createPotentialClient
p "Step 1: Attempting to save potential client to database"
end
end
我想测试createPotentialClient
是否被称为:
require 'rails_helper'
RSpec.describe HubspotFormSubmissionService, type: :model do
describe '#call' do
let(:form_data) { {
"first_name"=>"Jeremy",
"message"=>"wqffew",
"referrer"=>"Another Client"
} }
it 'attempts to process the form data' do
expect(HubspotFormSubmissionService).to receive(:createPotentialClient)
HubspotFormSubmissionService.new(form_data).call
end
end
end
我应该采取什么不同的做法?
您可以这样设置主题。然后在测试中,期望受试者收到的方法就像你在被嘲笑后得到的方法一样。我还将对createPotentialClient
进行单独的测试,以测试它是否返回了您期望的值。
subject { described_class.call }
before do
allow(described_class).to receive(:createPotentialClient)
end
it 'calls the method' do
expect(described_class).to receive(:createPotentialClient)
subject
end