後如何刪除(在數據庫和內存)創建RSpec的:平表/銷燬對象,每個上下文
- 每個測試
- 和每個上下文後後的對象嗎? (在上下文中,它可以在彼此之間構建測試)
是否有一種方法可以自動執行此操作?
我有以下問題:
每個測試條目保存到數據庫中。接下來的測試取決於這些條目。即使我想構建依賴於其他測試的測試,我也無法這樣做,因爲測試執行的順序不可控。
factories.rb:
sequence(:name) { |n| "purchaser #{n}" }
organization_spec.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 ==)
https://github.com/bmabey/database_cleaner – deefour