Module: Gemika::RSpec

Extended by:
RSpec
Included in:
RSpec
Defined in:
lib/gemika/rspec.rb

Instance Method Summary collapse

Instance Method Details

#binary(options = {}) ⇒ Object

Returns the binary name for the current RSpec version.



33
34
35
36
37
38
39
# File 'lib/gemika/rspec.rb', line 33

def binary(options = {})
  if Env.gem?('rspec', '< 2', options)
    'spec'
  else
    'rspec'
  end
end

#configure(&block) ⇒ Object

Configures RSpec.

Works with both RSpec 1 and RSpec 2.



46
47
48
# File 'lib/gemika/rspec.rb', line 46

def configure(&block)
  configurator.configure(&block)
end

#configure_clean_database_before_exampleObject

Configures RSpec to clean out the database before each example.

Requires the database_cleaner gem to be added to your development dependencies.



55
56
57
58
59
60
61
62
63
# File 'lib/gemika/rspec.rb', line 55

def configure_clean_database_before_example
  require 'database_cleaner' # optional dependency
  configure do |config|
    config.before(:each) do
      # Truncation works across most database adapters; I had issues with :deletion and pg
      DatabaseCleaner.clean_with(:truncation)
    end
  end
end

#configure_should_syntaxObject

Configures RSpec so it allows the should syntax that works across all RSpec versions.



68
69
70
71
72
73
74
75
76
77
# File 'lib/gemika/rspec.rb', line 68

def configure_should_syntax
  if Env.gem?('rspec', '>= 2.11')
    configure do |config|
      config.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }
      config.mock_with(:rspec) { |c| c.syntax = [:should, :expect] }
    end
  else
    # We have an old RSpec that only understands should syntax
  end
end

#run_specs(options = nil) ⇒ Object

Runs the RSpec binary.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gemika/rspec.rb', line 10

def run_specs(options = nil)
  options ||= {}
  files = options.fetch(:files, 'spec')
  rspec_options = options.fetch(:options, '--color')
  # We need to override the gemfile explicitely, since we have a default Gemfile in the project root
  gemfile = options.fetch(:gemfile, Gemika::Env.gemfile)
  fatal = options.fetch(:fatal, true)
  runner = binary(:gemfile => gemfile)
  bundle_exec = options.fetch(:bundle_exec) ? 'bundle exec' : nil
  command = [bundle_exec, runner, rspec_options, files].compact.join(' ')
  result = shell_out(command)
  if result
    true
  elsif fatal
    raise RSpecFailed, "RSpec failed: #{command}"
  else
    false
  end
end