Using Context as an example group in rSpec

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

end
end

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 :describe
end

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!

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Joe Ocampo

My personal philosophy is simple: "Have a good strategy that sets the environment for success through the enablement of the whole. Be agile but with a mind towards pragmatism. Delegate to the best qualified individuals, but don’t be afraid to involve yourself in all parts of a job. Treat everyone with respect, humility, and with a genuine pursuit towards excellence." Respected business and technical leader with expertise in directing organization towards effective results driven outcomes. Proven ability to perform and communicate from both technical and business perspectives. Strong technical and business acumen developed through experience, education and training. Provides the ability to utilize technology, harness business intelligence and execute strategically by optimizing systems, tools and process. Passionate about building people, companies and software by containing cost, maximizing operational throughput and capitalize on revenue. Looks to leverage the strengths of individuals and grow the organization to their maximum potential by harnessing the power of their collective whole and deliver results. Co-Founder of LosTechies.com
This entry was posted in rSpec, Ruby. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Comments are closed.