# Perche' database cleaner non pulisce?

**URL:** <https://www.ruby-forum.com/t/perche-database-cleaner-non-pulisce/207787>\
**Category:** IT\
**Created:** [June 2, 2011, 3:52pm UTC](https://www.ruby-forum.com/t/perche-database-cleaner-non-pulisce/207787 "2011-06-02T15:52:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 2, 2011, 3:52pm UTC](https://www.ruby-forum.com/t/perche-database-cleaner-non-pulisce/207787/1 "2011-06-02T15:52:03Z")

</div>

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?
