Module: Mongrel2::SpecHelpers

Defined in:
lib/mongrel2/testing.rb

Overview

A collection of helper functions that are generally useful for testing Mongrel2::Handlers.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(context) ⇒ Object

Inclusion callback – install some hooks



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongrel2/testing.rb', line 21

def self::included( context )

  context.before( :all ) do
    setup_config_db
  end

  context.around( :each ) do |example|
    if (( setting = example.[:db] ))
      if setting == :no_transaction || setting == :without_transaction
        Loggability[ Mongrel2 ].debug "  running without a transaction"
        example.run
      else
        Loggability[ Mongrel2 ].debug "  running with a transaction"
        Mongrel2::Config.db.transaction( rollback: :always ) do
          example.run
        end
      end
    else
      example.run
    end
  end

  super
end

Instance Method Details

#clean_config_dbObject

Truncate all the tables in the current config DB.



55
56
57
# File 'lib/mongrel2/testing.rb', line 55

def clean_config_db
  Mongrel2::Config.db.tables.collect {|t| Mongrel2::Config.db[t] }.each( &:truncate )
end

#setup_config_dbObject

Set up a Mongrel2 configuration database in memory.



48
49
50
51
# File 'lib/mongrel2/testing.rb', line 48

def setup_config_db
  Mongrel2::Config.db ||= Mongrel2::Config.in_memory_db
  Mongrel2::Config.init_database!
end