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!


Posted Feb 27 2008, 04:22 PM by Joe Ocampo
Filed under: ,

Comments

DotNetKicks.com wrote Using Context as an example group in rSpec
on 02-29-2008 12:35 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Aron wrote re: Using Context as an example group in rSpec
on 05-03-2009 4:05 AM

<a href= orlandorestaurante.statesaua.info >orlando restaurante</a>  

<a href= cheaphotels.statesaua.info >cheap hotels</a>  

<a href= noticierostelevisa.statesaua.info >noticieros televisa</a>  

<a href= tatuajestobillo.statesaua.info >tatuajes tobillo</a>  

<a href= huevocartoon.statesaua.info >huevo cartoon</a>  

<a href= caixalaietana.statesaua.info >caixa laietana</a>  

<a href= dibujohadaduendegnomos.statesaua.info >dibujo hada duende gnomos</a>  

<a href= fotohomenspeludo.statesaua.info >foto homens peludo</a>  

<a href= reservarhotelmarbella.statesaua.info >reservar hotel marbella</a>  

<a href= partituratablatura.statesaua.info >partitura tablatura</a>  

<a href= humanresourcesmanagement.statesaua.info >human resources management</a>  

<a href= shippingcar.statesaua.info >shipping car</a>  

<a href= chatgaycl.statesaua.info >chat gay cl</a>  

<a href= previsionmeteorologicasemanasanta.statesaua.info >prevision meteorologica semana santa</a>  

<a href= bridaljewelry.statesaua.info >bridal jewelry</a>  

<a href= phonecellular.statesaua.info >phone cellular</a>  

<a href= onlinedegreeprogram.statesaua.info >online degree program</a>  

<a href= hotelavenezia.statesaua.info >hotel a venezia</a>  

<a href= lecturatarotgratis.statesaua.info >lectura tarot gratis</a>  

<a href= dragontattoo.statesaua.info >dragon tattoo</a>  

Aron wrote re: Using Context as an example group in rSpec
on 05-03-2009 4:05 AM

<a href= orlandorestaurante.statesaua.info >orlando restaurante</a>  

<a href= cheaphotels.statesaua.info >cheap hotels</a>  

<a href= noticierostelevisa.statesaua.info >noticieros televisa</a>  

<a href= tatuajestobillo.statesaua.info >tatuajes tobillo</a>  

<a href= huevocartoon.statesaua.info >huevo cartoon</a>  

<a href= caixalaietana.statesaua.info >caixa laietana</a>  

<a href= dibujohadaduendegnomos.statesaua.info >dibujo hada duende gnomos</a>  

<a href= fotohomenspeludo.statesaua.info >foto homens peludo</a>  

<a href= reservarhotelmarbella.statesaua.info >reservar hotel marbella</a>  

<a href= partituratablatura.statesaua.info >partitura tablatura</a>  

<a href= humanresourcesmanagement.statesaua.info >human resources management</a>  

<a href= shippingcar.statesaua.info >shipping car</a>  

<a href= chatgaycl.statesaua.info >chat gay cl</a>  

<a href= previsionmeteorologicasemanasanta.statesaua.info >prevision meteorologica semana santa</a>  

<a href= bridaljewelry.statesaua.info >bridal jewelry</a>  

<a href= phonecellular.statesaua.info >phone cellular</a>  

<a href= onlinedegreeprogram.statesaua.info >online degree program</a>  

<a href= hotelavenezia.statesaua.info >hotel a venezia</a>  

<a href= lecturatarotgratis.statesaua.info >lectura tarot gratis</a>  

<a href= dragontattoo.statesaua.info >dragon tattoo</a>  

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems