你如何删除你创建的对象(在数据库和内存中)
- 每次测试后
- 在每个上下文之后?(在上下文中,相互构建测试可能是有意义的)
有没有一种方法可以自动执行此操作?
我有以下问题:
每个测试都将条目保存到数据库中。然后下一个测试取决于这些条目。即使我想构建依赖于其他测试的测试,我也做不到,因为测试执行的顺序是不可控的。
工厂.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 ==)