Module: Spec::Runner

Defined in:
lib/spec/runner.rb,
lib/spec/runner/context.rb,
lib/spec/runner/reporter.rb,
lib/spec/runner/spec_parser.rb,
lib/spec/runner/command_line.rb,
lib/spec/runner/context_eval.rb,
lib/spec/runner/spec_matcher.rb,
lib/spec/runner/heckle_runner.rb,
lib/spec/runner/option_parser.rb,
lib/spec/runner/specification.rb,
lib/spec/runner/context_runner.rb,
lib/spec/runner/drb_command_line.rb,
lib/spec/runner/backtrace_tweaker.rb,
lib/spec/runner/execution_context.rb,
lib/spec/runner/heckle_runner_win.rb,
lib/spec/runner/formatter/html_formatter.rb,
lib/spec/runner/formatter/rdoc_formatter.rb,
lib/spec/runner/spec_should_raise_handler.rb,
lib/spec/runner/formatter/specdoc_formatter.rb,
lib/spec/runner/formatter/base_text_formatter.rb,
lib/spec/runner/formatter/progress_bar_formatter.rb

Overview

Contexts and Specifications

Rather than expressing examples in classes, RSpec uses a custom domain specific language to express examples using contexts and specifications.

A context 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.

context "A new account" do

  setup do
    @account = Account.new
  end

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

end

We use the setup block to set up the context (given), and then the specify 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 setup, teardown, context_setup and context_teardown within a context:

context "..." do
  context_setup do
    ...
  end

  setup do
    ...
  end

  specify "number one" do
    ...
  end

  specify "number two" do
    ...
  end

  teardown do
    ...
  end

  context_teardown do
    ...
  end

end

The setup block will run before each of the specs, once for each spec. Likewise, the teardown block will run after each of the specs.

It is also possible to specify a context_setup and context_teardown block that will run only once for each context, respectively before the first setup and after the last teardown. The use of these is generally discouraged, because it introduces dependencies between the specs. 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:

context "..." do

  specify "..." 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

context "A new account" do
  include AccountExampleHelperMethods
  setup do
    @account = Account.new
  end

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

Defined Under Namespace

Modules: ContextEval, Formatter Classes: BacktraceTweaker, CommandLine, Context, ContextEvalModule, ContextRunner, DrbCommandLine, ExecutionContext, HeckleRunner, Heckler, NoisyBacktraceTweaker, OptionParser, QuietBacktraceTweaker, Reporter, SpecMatcher, SpecParser, SpecShouldRaiseHandler, Specification