Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rspec:如何重构类似的测试?(举例说明)_Rspec - Fatal编程技术网

Rspec:如何重构类似的测试?(举例说明)

Rspec:如何重构类似的测试?(举例说明),rspec,Rspec,我有两个例子,但我觉得里面的大部分代码都是一样的。但是,它们有点不同(记录略有不同,第二个记录中还有一个附加的断言)。我仍然是一个测试新手,所以在我前进的过程中寻找一些提示。我正在测试一个rake任务。这是我的密码: it 'leaves one billing info for each order' do order = FactoryGirl.create(:order) FactoryGirl.create_list(:billing_info, 2, order_id: ord

我有两个例子,但我觉得里面的大部分代码都是一样的。但是,它们有点不同(记录略有不同,第二个记录中还有一个附加的断言)。我仍然是一个测试新手,所以在我前进的过程中寻找一些提示。我正在测试一个rake任务。这是我的密码:

it 'leaves one billing info for each order' do
  order = FactoryGirl.create(:order)
  FactoryGirl.create_list(:billing_info, 2, order_id: order.id)

  expect(BillingInfo.all.count).to eq(2)

  run_rake_task

  expect(BillingInfo.all.count).to eq(1)
end

it 'keeps the billing info with trevance information' do
  order = FactoryGirl.create(:order)
  FactoryGirl.create(:billing_info, order_id: order.id, complete_trevance_message: nil, trevance_attempts: nil)
  FactoryGirl.create(:billing_info, order_id: order.id, complete_trevance_message: "303 -- Processor Decline", trevance_attempts: 1)

  expect(BillingInfo.all.count).to eq(2)

  run_rake_task

  expect(BillingInfo.all.count).to eq(1)
  expect(BillingInfo.first.complete_trevance_message).to eq("303 -- Processor Decline")
end

正如你所看到的,它们非常相似。像这样把它们一分为二可以吗?或者有更好的方法吗?

在我看来,干法考试并不总是最好的规则。这通常意味着您必须在方法中隐藏一些代码,以便更难阅读。所以我不会做太多更改,但有些事情可以用更简单的方式完成

context "billing rake task"
  let(:order) { FactoryGirl.create(:order) }

  it 'leaves one billing info for each order' do
    FactoryGirl.create_list(:billing_info, 2, order_id: order.id)
    expect { run_rake_task }.to change { BillingInfo.count }.by(-1)
  end

  it 'keeps the billing info with trevance information' do
    FactoryGirl.create(:billing_info, order_id: order.id, complete_trevance_message: nil, trevance_attempts: nil)
    FactoryGirl.create(:billing_info, order_id: order.id, complete_trevance_message: "303 -- Processor Decline", trevance_attempts: 1)

    expect { run_rake_task }.to change { BillingInfo.count }.by(-1)
    expect(BillingInfo.first.complete_trevance_message).to eq("303 -- Processor Decline")
  end
end

请注意,这稍微改变了规范,所以您不需要检查计数是否从正好2变为正好1,而是变为少1。我认为在这里更好,但我不能确定,因为我不太了解您的应用程序。

我将重构为如下内容:

let(:order) { FactoryGirl.create(:order) }

describe "description here" do

  before do
    FactoryGirl.create_list(:billing_info, 2, order_id: order.id)  
  end

  it 'leaves one billing info for each order' do
    expect(BillingInfo.all.count).to eq(2)

    run_rake_task  

    expect(BillingInfo.all.count).to eq(1)
  end

end

describe "description here" do

  before do
    FactoryGirl.create(:billing_info, order_id: order.id, complete_trevance_message: nil, trevance_attempts: nil)
    FactoryGirl.create(:billing_info, order_id: order.id, complete_trevance_message: "303 -- Processor Decline", trevance_attempts: 1)
  end

  it 'keeps the billing info with trevance information' do
    expect(BillingInfo.all.count).to eq(2)

    run_rake_task

    expect(BillingInfo.all.count).to eq(1)
    expect(BillingInfo.first.complete_trevance_message).to eq("303 -- Processor Decline")
  end

end

before
hook将在将来根据您的需要为您提供更大的灵活性。

@Edmund-虽然关于干燥和测试的观点已经很好地理解了,但如果不是当前参考,您可能希望查看以备将来使用。在第二种情况下实现这一独特的最终期望将是一件棘手的事情,可能不值得这样做,但剩下的事情非常简单。谢谢大家的提示。