Ruby on rails 如何跳过Rails规范中的部分方法代码

Ruby on rails 如何跳过Rails规范中的部分方法代码,ruby-on-rails,rspec,Ruby On Rails,Rspec,我有这个方法 def finalize_inquiry_process(form) if finalize_process == true inquiry_process.campaign_code.update(state: 'used') document_creator_class = InquiryProcessDocumentCreatorFetcher.new(inquiry_process).call document_creator_class.new

我有这个方法

def finalize_inquiry_process(form)
  if finalize_process == true
    inquiry_process.campaign_code.update(state: 'used')
    document_creator_class = InquiryProcessDocumentCreatorFetcher.new(inquiry_process).call
    document_creator_class.new(inquiry_process).call
  end
  Success(form)
end
我想在规范中跳过这一部分,这是一个真正的麻烦制造者,实现是一个不必要的时间浪费(pdf生成器,具有大量字段)

为此,我写了一个规范:

  let(:fetcher_instance) { instance_double(InquiryProcessDocumentCreatorFetcher) }

  before do
    allow(InquiryProcessDocumentCreatorFetcher).to receive(:new).and_return(fetcher_instance)
    allow(fetcher_instance).to receive(:call).and_return(nil)
  end

  it 'updates state of assigned campain code' do
    updated_inquiry_process = process_update.value!
    expect(updated_inquiry_process.campaign_code.state).to eq('used')
  end
end
InquiryProcesses::Update.call campain代码更新分配的campain代码的状态 失败/错误:文档\u创建者\u类。新建(查询\u过程)。调用

命名错误: nil:NilClass的未定义方法“new”


有没有可能跳过规范中的这部分代码?

好的,我使用
receive\u message\u chain
helper来管理它。规格应如下所示:

describe 'finalize inquiry process' do
  subject(:process_update) do
    described_class.new(
      inquiry_process: inquiry_process,
      form: loan_application_inquiry_process_update_form,
      finalize_process: true,
    ).call
  end

  let!(:inquiry_process) do
    create :inquiry_process, inquiry_template: loan_inquiry_template, campaign_code_uid: campaign_code.uid
  end

  before do
    allow(InquiryProcessDocumentCreatorFetcher).to receive_message_chain(:new, :call, :new, :call)
  end

  it 'updates state of assigned campain code' do
    updated_inquiry_process = process_update.value!
    expect(updated_inquiry_process.campaign_code.state).to eq('used')
  end
end

您可以通过依赖项注入试试运气: (这是一个粗略的草图,但我不知道系统的上下文,甚至不知道整个班级)

然后

let(:creator_fetcher) { instance_double(InquiryProcessDocumentCreatorFetcher) }
let(:document_creator_class) { instance_double(whatever_fetcher_call_returns_or_just_unveryfying_double) }

before { allow(creator_fetcher.to_receive(:call).and_return(document_creator_class) }

subject(:process_update) do
  described_class.new(
    inquiry_process: inquiry_process,
    form: loan_application_inquiry_process_update_form,
    finalize_process: true,
  ).call
end
无论如何,您的测试问题表明您的代码不是用测试编写的,这是一个糟糕的设计


间接(这里的依赖注入)可能有助于解决混乱

就我个人而言,我会将代码转移到另一个方法,并在测试中存根该方法。通常,最好避免留下方法链,因为您的测试最终取决于实现。
def initialize(inquiry_process:, form:, finalize_process:,  creator_fetcher:) 
  @creator_fetcher = creator_fetcher
  # all the other initializetions
  # maybe you can initialize creator_fetcher outside, and no need to pass inquiry_process anymore?
end

def creator_fetcher
  @creator_fetcher ||= InquiryProcessDocumentCreatorFetcher.new(inquiry_process)
end

def finalize_inquiry_process(form)
  if finalize_process == true
    inquiry_process.campaign_code.update(state: 'used')
    document_creator_class = creator_fetcher.call
    document_creator_class.new(inquiry_process).call
  end
  Success(form)
end
let(:creator_fetcher) { instance_double(InquiryProcessDocumentCreatorFetcher) }
let(:document_creator_class) { instance_double(whatever_fetcher_call_returns_or_just_unveryfying_double) }

before { allow(creator_fetcher.to_receive(:call).and_return(document_creator_class) }

subject(:process_update) do
  described_class.new(
    inquiry_process: inquiry_process,
    form: loan_application_inquiry_process_update_form,
    finalize_process: true,
  ).call
end