October 17, 2013

JavaScript in integration tests - part II

In my previous post about javascript in integration tests I've recommended to use shared database connection for your tests.

Well this is not true anymore. Why?

Because of this:

message type 0x54 arrived from server while idle
message type 0x44 arrived from server while idle
message type 0x43 arrived from server while idle
message type 0x5a arrived from server while idle

From time to time I've experienced strange errors in my tests. They were failing, or there were strange errors (like that listed above) which caused RSpec to froze...

This was because of shared database connection. And I did not like it at all. So I've dropped shared connection at all.

But what is the better way?

It is database_cleaner. With this gem you can easily setup different database cleanup strategies for your tests.

For one of my projects I've just used this setup:

# spec/spec_helper

RSpec.configure do |config|
  ...
  # set strategy and cleanup DB before running entire test suite
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end

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

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

That's all! This setup is pretty simple, but you have much more possibilities.

Check this article for some introduction, or database_cleaner github page for comprehensive information.

Hey there!

My name is Patrik Bóna and I am the only programmer at Memberful. This blog is kind of dead, but I just started my own Ruby on Rails screencast. Follow me on Twitter if you want to be notified about my newest videos.