Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:53:01+00:00 2026-06-06T09:53:01+00:00

I am having trouble parameterizing a shared_example in my rspec controller testing. My code

  • 0

I am having trouble parameterizing a shared_example in my rspec controller testing. My code is based on Passing Examples to a Shared Group as outlined on Relish

On the face of it, it seems like instance variables created in a shared_context are not available in shared_examples. Any ideas?

I am using Rails 3.1, with CanCan and Devise gems.

Controller (app/controllers/stores_controller.rb):

class StoresController < SubDomainController
  def index
    authorize! :list, Store
    @stores = current_user.company.stores.allrespond_to do |format|
      format.html # index.html.erb
      format.json { render json: @stores }
    end
  end
end

Shared Module (spec/support/shared.rb):

  module Shared
    shared_context "stores controller" do    
      before :all do    
        build_companies
        build_stores
        build_users
      end
    end

    shared_context "admin logged in" do   
      before :each do
        login_admin
      end    
    end

    #########THIS IS THE TEST THAT IS FAILING#########
    shared_examples "authorised_user" do |values|

      it "should assign correct variables" do
        values.each do |key,val|
          assigns[key].should == values[key]
        end
      end
    end
  end

Helpers Module (spec/support/helpers.rb):

module Helpers

  def build_companies
    @company = build_company(get_rand_string() ,"Correct")
    @other_company = build_company(get_rand_string() ,"Invalid")
  end

  def build_stores
    @store = FactoryGirl.create(:store, :company_id => @company.id)
    @other_store = FactoryGirl.create(:store, :company_id => @company.id)
    @other_company_store = FactoryGirl.create(:store, :company_id => @other_company.id)
  end

  def build_users
    @admin_user = FactoryGirl.create(:user, :role_id => 1,  :company => @company, :store_id => @store.id)
    @manager_user = FactoryGirl.create(:user, :role_id=> 2,  :company => @company, :store_id => @store.id)
    @regular_user = FactoryGirl.create(:user, :role_id=> 3,  :company => @company, :store_id => @store.id)
  end       
end

Spec Helpers (spec/spec_helper.rb):

.....
  config.include  Helpers
  config.include  Shared
.... 

Controller Spec (spec/controllers/stores_controller_spec.rb):
require ‘spec_helper’

describe StoresController do    
  include_context "stores controller"
    describe "INDEX" do

        [:admin].each do |u|
          describe "#{u} authorised" do          
            include_context "#{u} logged in"          
              before :each do
                get :index
              end

              ###passing instance variables to Helper module not working######
              it_behaves_like "authorised_user", {:stores => [@store, @other_store]}      

            end
          end
        end
    end 
end

Errors:

1) StoresController INDEX company stores admin authorised behaves like authorised_user should assign correct variables
   Failure/Error: assigns[key].should == values[key]
   expected: [nil, nil]
        got: [#<Store id: 1, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">, #<Store id: 2, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">] (using ==)                                                                                                                         
   Diff:
   @@ -1,2 +1,3 @@
   -[nil, nil]
   +[#<Store id: 1, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">,                                                                                                                                             
   + #<Store id: 2, company_id: 1, location_name: "MyString", address: "MyString", created_at: "2012-06-18 05:29:19", updated_at: "2012-06-18 05:29:19">]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:53:03+00:00Added an answer on June 6, 2026 at 9:53 am

    Figured this out myself.

    The variable needs to be set using the ‘let’ syntax in stores_controller_spec.rb as follows

    it_behaves_like "authorised_user" do
       let(:variable){stores}
       let(:values){[@store, @other_store]} 
    end
    

    Then in the shared_examples.rb:

    shared_examples "authorised_user" do
    
      it "should assign correct variables" do
          assigns[variable].should == values
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having trouble where I think I need to provide most of my code to
Having trouble with this code, wondering if I'm trying to achieve something impossible. I'm
Having trouble getting the javascript alert to display from my code behind. c# -
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble getting one portion of my code to work. Building a rudimentary linked
Having trouble with the examples for the .RowStart method. Comparing the 2 syntaxes: http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax/
Im having trouble with my Post and Comment to Post section of code. I
Having trouble with each function... Will try to explain by example... In my code,
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.