14

你如何删除你创建的对象(在数据库和内存中)

  • 每次测试后
  • 在每个上下文之后?(在上下文中,相互构建测试可能是有意义的)

有没有一种方法可以自动执行此操作?

我有以下问题:

每个测试都将条目保存到数据库中。然后下一个测试取决于这些条目。即使我想构建依赖于其他测试的测试,我也做不到,因为测试执行的顺序是不可控的。

工厂.rb:

sequence(:name) { |n| "purchaser #{n}" }      

组织规范.rb:

context "when no supplier exists" do
  it "finds no associated suppliers" do
    purchaser = create(:organization_purchaser)                
    purchaser.partners.empty?.should == true
  end
end

context "when one supplier exists" do
  it "finds one associated suppliers" do
    purchaser = create(:organization_purchaser)      
    supplier = create(:organization_supplier)
    partnership = create(:partnership, organization: purchaser, partner: supplier)         
    purchaser.partners.last.name.should == "purchaser 1"
  end
end

context "when two suppliers exist" do        
  it "finds two associated suppliers" do
    purchaser = create(:organization_purchaser)      
    2.times do |i|
      supplier = create(:organization_supplier)
      partnership = create(:partnership, organization: purchaser, partner: supplier) 
    end    
    purchaser.partners.last.name.should == "purchaser 2"
  end
end

RSpec 输出:

Organization
  #suppliers_for_purchaser
    responds
    when no supplier exists
      finds no associated suppliers
    when two suppliers exist
      finds two associated suppliers
    when one supplier exists
      finds one associated suppliers (FAILED - 1)

Failures:

1) Organization#suppliers_for_purchaser when one supplier exists finds one associated suppliers
 Failure/Error: purchaser.partners.last.name.should == "purchaser 1"
   expected: "purchaser 1"
        got: "purchaser 3" (using ==)
4

3 回答 3

16

您应该使用数据库清理器

您所要做的就是将以下代码添加到您的 Rspec 配置文件中spec_helper.rb

config.before(:suite) do
  DatabaseCleaner.strategy = :transaction
  DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

更新

从 Rails 5.1 开始,如果您使用,则不需要config.use_transactional_tests

https://github.com/rails/rails/pull/19282

于 2013-01-16T20:13:54.230 回答
8

您是否尝试添加 before 方法?

describe MyController do

before(:each) do
  User.delete_all
  MyOtherModel.delete_all
  ...
end
于 2013-01-16T19:51:21.833 回答
1

我认为您的问题更像是如何重置工厂女孩序列?那些没有存储在数据库中,即使你删除所有你仍然会遇到问题。在测试这样的东西时,我发现覆盖工厂女孩序列更容易..

it "finds one associated suppliers" do
    purchaser = create(:organization_purchaser)      
    supplier = create(:organization_supplier , name: "My First Supplier")
    partnership = create(:partnership, organization: purchaser, partner: supplier)         
    purchaser.partners.last.name.should == "My First Supplier"
  end

你可能做的其他事情是

it "finds one associated suppliers" do
        purchaser = create(:organization_purchaser)      
        supplier = create(:organization_supplier)
        partnership = create(:partnership, organization: purchaser, partner: supplier)         
        purchaser.partners.last.should == supplier 
      end

甚至

it "finds one associated suppliers" do
        purchaser = create(:organization_purchaser)      
        supplier = create(:organization_supplier)
        partnership = create(:partnership, organization: purchaser, partner: supplier)         
        purchaser.partners.last.name.should == supplier.name
      end
于 2013-01-16T20:00:09.977 回答