简体   繁体   中英

What happens that slow down my specs?

I am using Ruby on Rails 3.2.2, FactoryGirl 3.1.0, FactoryGirlRails 3.1.0, Rspec 2.9.0 and RspecRails 2.9.0. In my spec files (about 1300 Examples each file), before each Example runs, I seed the database and create and store few records using factories:

# spec/spec_helper.rb

config.before(:suite) do
  # Cleans the database.
  DatabaseCleaner.strategy = :transaction
  DatabaseCleaner.clean_with(:truncation)

  # Seeds the database.
  load "#{Rails.root}/db/seeds.rb"
end

I would like to know why, when I run the rspec ./spec/models/my_model_spec.rb command in the Terminal window, my specs start to run quickly but after a bit (that is, after it has run about 300 Examples) those go slowly . My examples are very simple and almost the same in terms of performance:

it "can not update title" do
  @article.update_attribute(:title, "Sample title").should be_false
end

After a certain number of tests, Examples like that above take 20/30 seconds to complete (!) and sometime also 60/120 seconds (!!!).

What happens after run about 300 Examples that slow down my specs also if those Examples are very simple and almost the same in terms of performance? Is there a reason to that behavior?


Note I : I tried to run specs in isolation and those are all fast.

Note II : When I run specs a ruby process in my "Activity Monitor" (I am using a Mac OS X running Snow Leopard 10.6.8) go to use 100% of CPU quickly.

Try cleaning your database after every example. Put this in your rspec_helper configure block:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM