Friday, January 21, 2011

In memory sqlite database for testing Rails 3 app with Cucumber and RSpec

Hello.
Let me introduce you my way of using in memory database inspite of existence of plenty articles regarding this on internets.

config/database.yml
test: &test
  adapter: sqlite3
  encoding: utf8
  database: ":memory:" 

In cucumber's features/support/env.rb in Before paste this:
load_schema = lambda {
    # use db agnostic schema by default
    load "#{Rails.root.to_s}/db/schema.rb" 

    # if you use seeds uncomment next line
    # load "#{Rails.root.to_s}/db/seeds.rb"
    # ActiveRecord::Migrator.up('db/migrate') # use migrations
  }
  silence_stream(STDOUT, &load_schema) 

Add lines below to spec/spec_helper.rb:
load_schema = lambda {
    load "#{Rails.root.to_s}/db/schema.rb" # use db agnostic schema by default
    # ActiveRecord::Migrator.up('db/migrate') # use migrations
  }
  silence_stream(STDOUT, &load_schema)