module NullDB::RSpec::NullifiedDatabase

Constants

NullDBAdapter

Public Class Methods

contextually_nullify_database(context) click to toggle source
# File lib/nulldb_rspec.rb, line 48
def self.contextually_nullify_database(context)
  nullify_database(context)
end
globally_nullify_database() click to toggle source
# File lib/nulldb_rspec.rb, line 39
def self.globally_nullify_database
  block = lambda { |config| nullify_database(config) }
  if defined?(RSpec)
    RSpec.configure(&block)
  else
    Spec::Runner.configure(&block)
  end
end

Private Class Methods

included(other) click to toggle source
# File lib/nulldb_rspec.rb, line 75
def self.included(other)
  if nullify_contextually?(other)
    contextually_nullify_database(other)
  else
    globally_nullify_database
  end
end
nullify_contextually?(other) click to toggle source
# File lib/nulldb_rspec.rb, line 83
def self.nullify_contextually?(other)
  if defined?(RSpec)
    other < RSpec::Core::ExampleGroup
  else
    other.is_a? Spec::ExampleGroup
  end
end
nullify_database(receiver) click to toggle source
# File lib/nulldb_rspec.rb, line 91
def self.nullify_database(receiver)
  receiver.before :all do
    ActiveRecord::Base.establish_connection(:adapter => :nulldb)
  end

  receiver.before :each do
    ActiveRecord::Base.connection.checkpoint!
  end

  receiver.after :all do
    ActiveRecord::Base.establish_connection(:test)
  end
end

Public Instance Methods

have_executed(entry_point) click to toggle source

A matcher for asserting that database statements have (or have not) been executed. Usage:

ActiveRecord::Base.connection.should have_executed(:insert)

The types of statement that can be matched mostly mirror the public operations available in ActiveRecord::ConnectionAdapters::DatabaseStatements:

  • :select_one

  • :select_all

  • :select_value

  • :insert

  • :update

  • :delete

  • :execute

There is also a special :anything symbol that will match any operation.

# File lib/nulldb_rspec.rb, line 69
def have_executed(entry_point)
  HaveExecuted.new(entry_point)
end