如何解决Rspec测试服务功能被调用
我有一项服务,我想测试一个函数是否被调用。我不确定如何测试它,因为似乎没有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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。