[rspec-users] [RSpec] Clarification on using shared_examples_for on a Rails Controller

Mark Wilden mark at mwilden.com
Wed Apr 22 14:07:52 EDT 2009

On Wed, Apr 22, 2009 at 10:04 AM, Stephen H. Gerstacker
<stephen at shortround.net> wrote:
> I have a Rails controller that I am using 'before_filter' on certain actions
> to assign some extra variables.  I noticed the 'shared_examples_for' method
> and thought this would be a good way to test that the given actions are
> loading what they need.  I wrote the following:
> shared_examples_for 'pages showing the yearly archive' do
>     it 'should assign a list of yearly archives' do
>       Factory.create(:post, :published_at => Time.local(2009,02,01))
>       Factory.create(:post, :published_at => Time.local(2009,01,01))
>       Factory.create(:post, :published_at => Time.local(2008,01,01))
>       Factory.create(:post, :published_at => Time.local(2007,01,01))
>       Factory.create(:post, :published_at => Time.local(2001,01,01))
>       get :index
>       assigns[:years].should == [ 2009, 2008, 2007, 2001 ]
>     end
>   end
> The problem is, it is specific to the 'index' action.  I can't use the
> 'it_should_behave_like "pages showing the yearly archive"' for other
> actions.

Assign the action to an instance variable and set it to the desired
value in a before(:all) in each of the sharers. In the shared example,
do get @action.


More information about the rspec-users mailing list