module Gemika::RSpec

Public Instance Methods

binary(options = {}) click to toggle source

Returns the binary name for the current RSpec version.

# File lib/gemika/rspec.rb, line 33
def binary(options = {})
  if Env.gem?('rspec', '< 2', options)
    'spec'
  else
    'rspec'
  end
end
configure(&block) click to toggle source

Configures RSpec.

Works with both RSpec 1 and RSpec 2.

# File lib/gemika/rspec.rb, line 46
def configure(&block)
  configurator.configure(&block)
end
configure_clean_database_before_example() click to toggle source

Configures RSpec to clean out the database before each example.

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

# 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_syntax() click to toggle source

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

# 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) click to toggle source

Runs the RSpec binary.

# 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

Private Instance Methods

configurator() click to toggle source
# File lib/gemika/rspec.rb, line 85
def configurator
  if Env.gem?('rspec', '<2')
    Spec::Runner
  else
    ::RSpec
  end
end
shell_out(command) click to toggle source
# File lib/gemika/rspec.rb, line 81
def shell_out(command)
  system(command)
end