Module: Spec::Runner

Defined in:
lib/gems/rspec-1.1.11/lib/spec/runner.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/options.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/reporter.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/spec_parser.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/command_line.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/heckle_runner.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/option_parser.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/options_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/reporter_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/drb_command_line.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/backtrace_tweaker.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/command_line_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/example_group_runner.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/output_one_time_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/drb_command_line_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/base_formatter.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/html_formatter.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/heckle_runner_unsupported.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/class_and_arguments_parser.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/profile_formatter.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/snippet_extractor.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/specdoc_formatter.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/base_text_formatter.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/text_mate_formatter.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/noisy_backtrace_tweaker_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/quiet_backtrace_tweaker_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/story/html_formatter.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/base_formatter_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/html_formatter_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/nested_text_formatter.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/class_and_argument_parser_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/progress_bar_formatter.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/profile_formatter_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/snippet_extractor_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/specdoc_formatter_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/spec_mate_formatter_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/failing_examples_formatter.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/story/plain_text_formatter.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/story/html_formatter_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/nested_text_formatter_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/story/progress_bar_formatter.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/progress_bar_formatter_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/runner/formatter/failing_example_groups_formatter.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/failing_examples_formatter_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/story/plain_text_formatter_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/story/progress_bar_formatter_spec.rb,
lib/gems/rspec-1.1.11/spec/spec/runner/formatter/failing_example_groups_formatter_spec.rb

Overview

ExampleGroups and Examples

Rather than expressing examples in classes, RSpec uses a custom DSLL (DSL light) to describe groups of examples.

A ExampleGroup is the equivalent of a fixture in xUnit-speak. It is a metaphor for the context in which you will run your executable example - a set of known objects in a known starting state. We begin be describing

describe Account do

  before do
    @account = Account.new
  end

  it "should have a balance of $0" do
    @account.balance.should == Money.new(0, :dollars)
  end

end

We use the before block to set up the Example (given), and then the #it method to hold the example code that expresses the event (when) and the expected outcome (then).

Helper Methods

A primary goal of RSpec is to keep the examples clear. We therefore prefer less indirection than you might see in xUnit examples and in well factored, DRY production code. We feel that duplication is OK if removing it makes it harder to understand an example without having to look elsewhere to understand its context.

That said, RSpec does support some level of encapsulating common code in helper methods that can exist within a context or within an included module.

Setup and Teardown

You can use before and after within a Example. Both methods take an optional scope argument so you can run the block before :each example or before :all examples

describe "..." do
  before :all do
    ...
  end

  before :each do
    ...
  end

  it "should do something" do
    ...
  end

  it "should do something else" do
    ...
  end

  after :each do
    ...
  end

  after :all do
    ...
  end

end

The before :each block will run before each of the examples, once for each example. Likewise, the after :each block will run after each of the examples.

It is also possible to specify a before :all and after :all block that will run only once for each behaviour, respectively before the first before :each and after the last after :each. The use of these is generally discouraged, because it introduces dependencies between the examples. Still, it might prove useful for very expensive operations if you know what you are doing.

Local helper methods

You can include local helper methods by simply expressing them within a context:

describe "..." do

  it "..." do
    helper_method
  end

  def helper_method
    ...
  end

end

Included helper methods

You can include helper methods in multiple contexts by expressing them within a module, and then including that module in your context:

module AccountExampleHelperMethods
  def helper_method
    ...
  end
end

describe "A new account" do
  include AccountExampleHelperMethods
  before do
    @account = Account.new
  end

  it "should have a balance of $0" do
    helper_method
    @account.balance.should eql(Money.new(0, :dollars))
  end
end

Shared Example Groups

You can define a shared Example Group, that may be used on other groups

share_examples_for "All Editions" do
  it "all editions behaviour" ...
end

describe SmallEdition do
  it_should_behave_like "All Editions"

  it "should do small edition stuff" do
    ...
  end
end

You can also assign the shared group to a module and include that

share_as :AllEditions do
  it "should do all editions stuff" ...
end

describe SmallEdition do
  it_should_behave_like AllEditions

  it "should do small edition stuff" do
    ...
  end
end

And, for those of you who prefer to use something more like Ruby, you can just include the module directly

describe SmallEdition do
  include AllEditions

  it "should do small edition stuff" do
    ...
  end
end

Defined Under Namespace

Modules: Formatter Classes: BacktraceTweaker, ClassAndArgumentsParser, CommandLine, DrbCommandLine, ExampleGroupRunner, HeckleRunner, Heckler, NoisyBacktraceTweaker, OptionParser, Options, QuietBacktraceTweaker, Reporter, SpecParser

Constant Summary collapse

BehaviourRunner =

TODO: BT - Deprecate BehaviourRunner?

ExampleGroupRunner

Class Method Summary collapse

Class Method Details

.configurationObject

:nodoc:



168
169
170
# File 'lib/gems/rspec-1.1.11/lib/spec/runner.rb', line 168

def configuration # :nodoc:
  @configuration ||= Spec::Example::Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Use this to configure various configurable aspects of RSpec:

Spec::Runner.configure do |configuration|
  # Configure RSpec here
end

The yielded configuration object is a Spec::Example::Configuration instance. See its RDoc for details about what you can do with it.

Yields:



183
184
185
# File 'lib/gems/rspec-1.1.11/lib/spec/runner.rb', line 183

def configure
  yield configuration
end

.optionsObject

:nodoc:



199
200
201
202
203
204
205
# File 'lib/gems/rspec-1.1.11/lib/spec/runner.rb', line 199

def options # :nodoc:
  @options ||= begin
    parser = ::Spec::Runner::OptionParser.new($stderr, $stdout)
    parser.order!(ARGV)
    parser.options
  end
end

.register_at_exit_hookObject

:nodoc:



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/gems/rspec-1.1.11/lib/spec/runner.rb', line 187

def register_at_exit_hook # :nodoc:
  unless @already_registered_at_exit_hook
    at_exit do
      unless $! || Spec.run? || Spec::Example::ExampleGroupFactory.all_registered?(options.example_groups)
        success = Spec.run
        exit success if Spec.exit?
      end
    end
    @already_registered_at_exit_hook = true
  end
end

.use(options) ⇒ Object



207
208
209
# File 'lib/gems/rspec-1.1.11/lib/spec/runner.rb', line 207

def use options
  @options = options
end