Module: Spec::Example

Defined in:
lib/vendor/plugins/rspec/lib/spec/example.rb,
lib/vendor/plugins/rspec/spec/spec_helper.rb,
lib/vendor/plugins/rspec/spec/spec_helper.rb,
lib/vendor/plugins/rspec/lib/spec/example/errors.rb,
lib/vendor/plugins/rspec/lib/spec/example/pending.rb,
lib/vendor/plugins/rspec/lib/spec/example/subject.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_group.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_proxy.rb,
lib/vendor/plugins/rspec/spec/spec/example/subject_spec.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_matcher.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_methods.rb,
lib/vendor/plugins/rspec/lib/spec/example/args_and_options.rb,
lib/vendor/plugins/rspec/lib/spec/example/predicate_matchers.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_group_proxy.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_group_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_proxy_spec.rb,
lib/vendor/plugins/rspec/lib/spec/example/module_reopening_fix.rb,
lib/vendor/plugins/rspec/lib/spec/example/shared_example_group.rb,
lib/vendor/plugins/rspec/spec/spec/example/pending_module_spec.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_group_factory.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_matcher_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_methods_spec.rb,
lib/vendor/plugins/rspec/lib/spec/example/before_and_after_hooks.rb,
lib/vendor/plugins/rspec/lib/spec/example/example_group_hierarchy.rb,
lib/vendor/plugins/rspec/spec/spec/example/predicate_matcher_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_group_proxy_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/nested_example_group_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/shared_example_group_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_group_factory_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_group_methods_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/subclassing_example_group_spec.rb,
lib/vendor/plugins/rspec/spec/spec/example/example_group_class_definition_spec.rb,
lib/vendor/plugins/rspec-rails/spec/spec/rails/example/example_group_factory_spec.rb

Overview

Example Groups and Code Examples

A Code Example is an executable example of how a bit of code is expected to behave.

An Example Group is a group of code examples.

RSpec exposes a DSL to describe groups of examples.

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

Before and After

You can use the before() and after() methods to extract common code within an Example Group. 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 example group, before the first before :each and after the last after :each respectively. 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 an example group:

describe "..." do

  it "..." do
    helper_method
  end

  def helper_method
    ...
  end

end

Included helper methods

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

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: ArgsAndOptions, BeforeAndAfterHooks, ExampleGroupMethods, ExampleMethods, ModuleReopeningFix, Pending, PredicateMatchers, Resettable, Subject Classes: ChildExampleGroup, ExampleClassVariablePollutionSpec, ExampleGroup, ExampleGroupDouble, ExampleGroupFactory, ExampleGroupHierarchy, ExampleGroupProxy, ExampleGroupSubclass, ExampleMatcher, ExampleModuleScopingSpec, ExamplePendingError, ExampleProxy, Fish, GrandParentExampleGroup, NoDescriptionError, NonStandardError, NotYetImplementedError, ParentExampleGroup, PendingExampleFixedError, SharedExampleGroup