If you try to do the following in rSpec you will receive a (nil:NilClass) error on the inner context in the ‘before’ statement when it tries to use @user.
describe User do before(:each) do @user = User.new end context "(adding assigned role)" do before(:each) do @user.assign_role("Manager") end specify "should be in any roles assigned to it" do @user.should be_in_role("Manager") end specify "should not be in any role not assigned to it" do @user.should_not be_in_role("unassigned role") end end context "(adding assigned group)" do endend
This perplexed me because the rDoc indicates that [context] method is an alias for [describe] method. Turns out there are two different places where describe is defined. One in main (the outermost layer) and one inside an ExampleGroup. The one in the example group isn’t aliased.
So to solve that for the short term in your own code you can do this in your spec_helper.rb file:
module Spec::Example::ExampleGroupMethods alias :context :describeend
In your spec file add the following to the header assuming the spec_helper.rb is in the same directory:
require File.dirname(__FILE__) + '/spec_helper'
Now everything should work out great! BDD sweetness! Here is the final user_spec.rb
require 'user'require File.dirname(__FILE__) + '/spec_helper' describe User do before(:each) do @user = User.new end context "(adding assigned role)" do before(:each) do @user.assign_role("Manager") end specify do @user.should be_in_role("Manager") end specify do @user.should_not be_in_role("unassigned role") end end describe "(adding assigned group)" do end end
Happy coding!
