简体   繁体   English

我应该如何使用rspec测试此类?

[英]How should I test this class using rspec?

I have created a service object that creates new interactions. 我创建了一个服务对象,该对象创建了新的交互。 When an interaction is created, this kicks off logic to generate a lead. 创建互动后,这将启动逻辑以生成潜在客户。 Leads are generated differently based on interaction_type. 潜在客户根据interact_type的生成方式有所不同。

See code below: 参见下面的代码:

class InteractionCreation

  def initialize params = {}
    @interaction = Interaction.new(params)    
  end

  def call
    if interaction.save
      generate_lead
    end

    return interaction
  end

  private

  def interaction
    @interaction
  end

  def generate_lead
    LeadGeneration.new(interaction).call
  end
end

I have tried a few things, but I am not sure how much I should be testing. 我已经尝试了一些方法,但是我不确定应该测试多少。 It's easy to test whether an interaction is generated or not, but should I be doing anything regarding the lead that is created by LeadGeneration. 测试是否生成交互很容易,但是我应该对LeadGeneration创建的销售线索做些什么。 I have tried to minimize the size of the public interface and only testing that right now. 我试图最小化公共接口的大小,并且仅在现在进行测试。 Any thoughts? 有什么想法吗?

I'd do at least this: 我至少会这样做:

describe InteractionCreation do

  let(:interaction) { double :interaction }
  let(:params)      { double :params }
  let(:action)      { described_class.new(params).call }
  let(:lead)        { double(:lead) }

  before do
    Interaction.should_receive(:new).with(params).and_return interaction
    LeadGeneration.stub(:new).and_return(lead)
  end

  it 'doesnt call Lead Generation when save ko' do
    interaction.stub(:save).and_return false
    lead.should_not_receive :call

    action
  end

  it 'calls Lead Generation when save ok' do
    interaction.stub(:save).and_return true
    lead.should_receive :call

    action
  end

end

Maybe return value should be check too, in this case, add another spec :) 也许返回值也应该检查,在这种情况下,添加另一个规范:)

You can check the public methods like 'initialize' and 'call' using rspec. 您可以使用rspec检查公共方法,例如“初始化”和“调用”。 Check if the initialize method assigns the attributes correctly. 检查initialize方法是否正确分配了属性。 for the 'interaction' method, you can check if 'generate_lead' is called if the interaction is saved. 对于“交互”方法,可以检查是否保存了交互,是否调用了“ generate_lead”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM