Ho due modelli, Sector e Category.
Category has_many :sectors.
Uso database_cleaner e rspec lo chiama includendo il seguente file:
RSpec.configure do |config|
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
end
Il test per il model Sector e’:
describe Sector do
before(:each) do
@attr = { :name => “Sector-1” }
end
it “should create a new instance given valid attributes” do
Sector.create!(@attr)
end
it “should require a name” do
no_name_sector = Sector.new(@attr.merge(:name => “”))
no_name_sector.should_not be_valid
end
end
Funziona e, finito il test, il db risulta pulito.
Il test per Category e’:
describe Category do
before(:each) do
sector = Sector.make!
@attr = { :name => “Category-1”, :sector => sector}
end
it “should create a new instance given valid attributes” do
Category.create!(@attr)
end
it “should require a name” do
no_name_category = Category.new(@attr.merge(:name => “”))
no_name_category.should_not be_valid
end
it “should require a Sector” do
no_sector_category = Category.new(@attr.merge(:sector => nil))
no_sector_category.should_not be_valid
end
end
Anche questo test passa ma mi ritrovo il db con 3 Sectors, quindi non
viene pulito.
Come mai?