Parent

Included Modules

Class/Module Index [+]

Quicksearch

RSpec::Core::Configuration

Public Class Methods

add_setting(name, opts={}) click to toggle source
# File lib/rspec/core/configuration.rb, line 9
def self.add_setting(name, opts={})
  if opts[:alias]
    alias_method name, opts[:alias]
    alias_method "#{name}=", "#{opts[:alias]}="
    alias_method "#{name}?", "#{opts[:alias]}?"
  else
    define_method("#{name}=") {|val| settings[name] = val}
    define_method(name)       { settings.has_key?(name) ? settings[name] : opts[:default] }
    define_method("#{name}?") { send name }
  end
end
new() click to toggle source
# File lib/rspec/core/configuration.rb, line 55
def initialize
  @color_enabled = false
  self.include_or_extend_modules = []
  self.files_to_run = []
  self.backtrace_clean_patterns = DEFAULT_BACKTRACE_PATTERNS.dup
  self.exclusion_filter = CONDITIONAL_FILTERS.dup
end

Public Instance Methods

add_formatter(formatter_to_use, path=nil) click to toggle source
# File lib/rspec/core/configuration.rb, line 277
def add_formatter(formatter_to_use, path=nil)
  formatter_class =
    built_in_formatter(formatter_to_use) ||
    custom_formatter(formatter_to_use) ||
    (raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?.")

  formatters << formatter_class.new(path ? file_at(path) : output)
end
Also aliased as: formatter=
add_setting(:name) click to toggle source
add_setting(:name, :default => "default_value")
add_setting(:name, :alias => :other_setting)

Use this to add custom settings to the RSpec.configuration object.

RSpec.configuration.add_setting :foo

Creates three methods on the configuration object, a setter, a getter, and a predicate:

RSpec.configuration.foo=(value)
RSpec.configuration.foo()
RSpec.configuration.foo?() # returns true if foo returns anything but nil or false

Intended for extension frameworks like rspec-rails, so they can add config settings that are domain specific. For example:

RSpec.configure do |c|
  c.add_setting :use_transactional_fixtures, :default => true
  c.add_setting :use_transactional_examples, :alias => :use_transactional_fixtures
end

Options

add_setting takes an optional hash that supports the following keys:

:default => "default value"

This sets the default value for the getter and the predicate (which will return true as long as the value is not false or nil).

:alias => :other_setting

Aliases its setter, getter, and predicate, to those for the other_setting.

# File lib/rspec/core/configuration.rb, line 105
def add_setting(name, opts={})
  self.class.add_setting(name, opts)
end
alias_example_to(new_name, *args) click to toggle source

E.g. alias_example_to :crazy_slow, :speed => 'crazy_slow' defines crazy_slow as an example variant that has the crazy_slow speed option

# File lib/rspec/core/configuration.rb, line 318
def alias_example_to(new_name, *args)
  extra_options = build_metadata_hash_from(args)
  RSpec::Core::ExampleGroup.alias_example_to(new_name, extra_options)
end
alias_it_should_behave_like_to(new_name, report_label = '') click to toggle source

Define an alias for it_should_behave_like that allows different language (like "it_has_behavior" or "it_behaves_like") to be employed when including shared examples.

Example:

alias_it_should_behave_like_to(:it_has_behavior, 'has behavior:')

allows the user to include a shared example group like:

describe Entity do
  it_has_behavior 'sortability' do
    let(:sortable) { Entity.new }
  end
end

which is reported in the output as:

Entity
  has behavior: sortability
    # sortability examples here
# File lib/rspec/core/configuration.rb, line 344
def alias_it_should_behave_like_to(new_name, report_label = '')
  RSpec::Core::ExampleGroup.alias_it_should_behave_like_to(new_name, report_label)
end
cleaned_from_backtrace?(line) click to toggle source
# File lib/rspec/core/configuration.rb, line 121
def cleaned_from_backtrace?(line)
  backtrace_clean_patterns.any? { |regex| line =~ regex }
end
color_enabled() click to toggle source
# File lib/rspec/core/configuration.rb, line 223
def color_enabled
  @color_enabled && output_to_tty?
end
color_enabled=(bool) click to toggle source
# File lib/rspec/core/configuration.rb, line 231
def color_enabled=(bool)
  return unless bool
  @color_enabled = true
  if bool && ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
    unless ENV['ANSICON']
      warn "You must use ANSICON 1.31 or later (http://adoxa.110mb.com/ansicon/) to use colour on Windows"
      @color_enabled = false
    end
  end
end
color_enabled?() click to toggle source
# File lib/rspec/core/configuration.rb, line 227
def color_enabled?
  color_enabled
end
configure_expectation_framework() click to toggle source
# File lib/rspec/core/configuration.rb, line 412
def configure_expectation_framework
  expectation_frameworks.each do |framework|
    RSpec::Core::ExampleGroup.send(:include, framework)
  end
end
configure_group(group) click to toggle source
# File lib/rspec/core/configuration.rb, line 401
def configure_group(group)
  include_or_extend_modules.each do |include_or_extend, mod, filters|
    next unless filters.empty? || group.apply?(:any?, filters)
    group.send(include_or_extend, mod)
  end
end
configure_mock_framework() click to toggle source
# File lib/rspec/core/configuration.rb, line 408
def configure_mock_framework
  RSpec::Core::ExampleGroup.send(:include, mock_framework)
end
debug=(bool) click to toggle source
# File lib/rspec/core/configuration.rb, line 250
def debug=(bool)
  return unless bool
  begin
    require 'ruby-debug'
    Debugger.start
  rescue LoadError
    raise #{'*'*50}You must install ruby-debug to run rspec with the --debug option.If you have ruby-debug installed as a ruby gem, then you need to eitherrequire 'rubygems' or configure the RUBYOPT environment variable withthe value 'rubygems'.#{'*'*50}
  end
end
exclusion_filter() click to toggle source
# File lib/rspec/core/configuration.rb, line 352
def exclusion_filter
  settings[:exclusion_filter] || {}
end
exclusion_filter=(filter) click to toggle source
# File lib/rspec/core/configuration.rb, line 348
def exclusion_filter=(filter)
  settings[:exclusion_filter] = filter
end
expect_with(*frameworks) click to toggle source

Sets the expectation framework module(s).

frameworks can be :rspec, :stdlib, or both

Given :rspec, configures rspec/expectations. Given :stdlib, configures test/unit/assertions Given both, configures both

# File lib/rspec/core/configuration.rb, line 200
def expect_with(*frameworks)
  settings[:expectation_frameworks] = []
  frameworks.each do |framework|
    case framework
    when Symbol
      case framework
      when :rspec
        require 'rspec/core/expecting/with_rspec'
        self.expecting_with_rspec = true
      when :stdlib
        require 'rspec/core/expecting/with_stdlib'
      else
        raise ArgumentError, "#{framework.inspect} is not supported"
      end
      settings[:expectation_frameworks] << RSpec::Core::ExpectationFrameworkAdapter
    end
  end
end
expectation_framework=(framework) click to toggle source

Delegates to expect_with()

# File lib/rspec/core/configuration.rb, line 189
def expectation_framework=(framework)
  expect_with([framework])
end
expectation_frameworks() click to toggle source

Returns the configured expectation framework adapter module(s)

# File lib/rspec/core/configuration.rb, line 183
def expectation_frameworks
  expect_with :rspec unless settings[:expectation_frameworks]
  settings[:expectation_frameworks]
end
extend(mod, *args) click to toggle source
# File lib/rspec/core/configuration.rb, line 396
def extend(mod, *args)
  filters = build_metadata_hash_from(args)
  include_or_extend_modules << [:extend, mod, filters]
end
files_or_directories_to_run=(*files) click to toggle source
# File lib/rspec/core/configuration.rb, line 299
def files_or_directories_to_run=(*files)
  self.files_to_run = files.flatten.collect do |file|
    if File.directory?(file)
      pattern.split(",").collect do |pattern|
        Dir["#{file}/#{pattern.strip}"]
      end
    else
      if file =~ /(\:(\d+))$/
        self.line_number = $2
        file.sub($1,'')
      else
        file
      end
    end
  end.flatten
end
filter_run(*args) click to toggle source
filter_run_excluding(*args) click to toggle source
# File lib/rspec/core/configuration.rb, line 386
def filter_run_excluding(*args)
  options = build_metadata_hash_from(args)
  self.exclusion_filter = (exclusion_filter || {}).merge(options)
end
filter_run_including(*args) click to toggle source
# File lib/rspec/core/configuration.rb, line 363
def filter_run_including(*args)
  force_overwrite = if args.last.is_a?(Hash) || args.last.is_a?(Symbol)
    false
  else
    args.pop
  end

  options = build_metadata_hash_from(args)

  if inclusion_filter[:line_number] || inclusion_filter[:full_description]
    warn "Filtering by #{options.inspect} is not possible since "                 "you are already filtering by #{inclusion_filter.inspect}"
  else
    if force_overwrite
      self.inclusion_filter = options
    else
      self.inclusion_filter = (inclusion_filter || {}).merge(options)
    end
  end
end
Also aliased as: filter_run
formatter=(formatter_to_use, path=nil) click to toggle source
Alias for: add_formatter
formatters() click to toggle source
# File lib/rspec/core/configuration.rb, line 288
def formatters
  @formatters ||= []
end
full_backtrace=(true_or_false) click to toggle source
# File lib/rspec/core/configuration.rb, line 219
def full_backtrace=(true_or_false)
  settings[:backtrace_clean_patterns] = true_or_false ? [] : DEFAULT_BACKTRACE_PATTERNS
end
full_description=(description) click to toggle source
# File lib/rspec/core/configuration.rb, line 273
def full_description=(description)
  filter_run({ :full_description => /#{description}/ }, true)
end
include(mod, *args) click to toggle source
# File lib/rspec/core/configuration.rb, line 391
def include(mod, *args)
  filters = build_metadata_hash_from(args)
  include_or_extend_modules << [:include, mod, filters]
end
inclusion_filter() click to toggle source
# File lib/rspec/core/configuration.rb, line 360
def inclusion_filter
  settings[:inclusion_filter] || {}
end
inclusion_filter=(filter) click to toggle source
# File lib/rspec/core/configuration.rb, line 356
def inclusion_filter=(filter)
  settings[:inclusion_filter] = filter
end
libs=(libs) click to toggle source
# File lib/rspec/core/configuration.rb, line 242
def libs=(libs)
  libs.map {|lib| $LOAD_PATH.unshift lib}
end
line_number=(line_number) click to toggle source
# File lib/rspec/core/configuration.rb, line 269
def line_number=(line_number)
  filter_run({ :line_number => line_number.to_i }, true)
end
load_spec_files() click to toggle source
# File lib/rspec/core/configuration.rb, line 418
def load_spec_files
  files_to_run.map {|f| load File.expand_path(f) }
  raise_if_rspec_1_is_loaded
end
mock_framework() click to toggle source

Returns the configured mock framework adapter module

# File lib/rspec/core/configuration.rb, line 126
def mock_framework
  settings[:mock_framework] ||= begin
                                  require 'rspec/core/mocking/with_rspec'
                                  RSpec::Core::MockFrameworkAdapter
                                end
end
mock_framework=(framework) click to toggle source

Sets the mock framework adapter module.

framework can be a Symbol or a Module.

Given any of :rspec, :mocha, :flexmock, or :rr, configures the named framework.

Given :nothing, configures no framework. Use this if you don't use any mocking framework to save a little bit of overhead.

Given a Module, includes that module in every example group. The module should adhere to RSpec's mock framework adapter API:

setup_mocks_for_rspec
  - called before each example

verify_mocks_for_rspec
  - called after each example. Framework should raise an exception
    when expectations fail

teardown_mocks_for_rspec
  - called after verify_mocks_for_rspec (even if there are errors)
# File lib/rspec/core/configuration.rb, line 160
def mock_framework=(framework)
  case framework
  when Module
    settings[:mock_framework] = framework
  when String, Symbol
    require case framework.to_s
            when /rspec/
              'rspec/core/mocking/with_rspec'
            when /mocha/
              'rspec/core/mocking/with_mocha'
            when /rr/
              'rspec/core/mocking/with_rr'
            when /flexmock/
              'rspec/core/mocking/with_flexmock'
            else
              'rspec/core/mocking/with_absolutely_nothing'
            end
    settings[:mock_framework] = RSpec::Core::MockFrameworkAdapter
  else
  end
end
mock_with(framework) click to toggle source

Delegates to mock_framework=(framework)

# File lib/rspec/core/configuration.rb, line 134
def mock_with(framework)
  self.mock_framework = framework
end
puts(message) click to toggle source
# File lib/rspec/core/configuration.rb, line 109
def puts(message)
  output_stream.puts(message)
end
reporter() click to toggle source
# File lib/rspec/core/configuration.rb, line 292
def reporter
  @reporter ||= begin
                  add_formatter('progress') if formatters.empty?
                  Reporter.new(*formatters)
                end
end
requires=(paths) click to toggle source
# File lib/rspec/core/configuration.rb, line 246
def requires=(paths)
  paths.map {|path| require path}
end
reset() click to toggle source
# File lib/rspec/core/configuration.rb, line 63
def reset
  @reporter = nil
end
settings() click to toggle source
# File lib/rspec/core/configuration.rb, line 113
def settings
  @settings ||= {}
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.