[rspec-users] Could not find shared example group named "an admin is logged in"

Justin Ko jko170 at gmail.com
Thu Oct 7 17:53:29 EDT 2010



On Oct 7, 5:36 pm, Tim Gremore <timgrem... at gmail.com> wrote:
> I'm stuck! Not sure what I'm missing but I'm struggling to get a shared
> example group working with my controller specs.  Here is a piece of the
> backtrace:
>
> /Users/20217633/.rvm/gems/ruby-1.9.2-p0 at rails3/gems/rspec-core-2.0.0.rc/lib /rspec/core/example_group.rb:68:in
> `it_should_behave_like': Could not find shared example group named "an admin
> is logged in" (RuntimeError)
>  from
> /Users/20217633/apps/curriculum/spec/controllers/users_controller_spec.rb:5 :in
> `block in <top (required)>'
> from /Users/20217633/.rvm/gems/ruby-1.9.2-p0 at rails3/gems/rspec-core-2.0.0.rc/lib /rspec/core/example_group.rb:132:in
> `module_eval'
>  from /Users/20217633/.rvm/gems/ruby-1.9.2-p0 at rails3/gems/rspec-core-2.0.0.rc/lib /rspec/core/example_group.rb:132:in
> `subclass'
> from /Users/20217633/.rvm/gems/ruby-1.9.2-p0 at rails3/gems/rspec-core-2.0.0.rc/lib /rspec/core/example_group.rb:119:in
> `describe'
>  from /Users/20217633/.rvm/gems/ruby-1.9.2-p0 at rails3/gems/rspec-core-2.0.0.rc/lib /rspec/core/extensions/object.rb:7:in
> `describe'
> from
> /Users/20217633/apps/curriculum/spec/controllers/users_controller_spec.rb:3 :in
> `<top (required)>'
>
> I'm using the following:
>
> rails 3.0
> rspec-rails 2.0.0.rc (and friends)
> cucumber-rails 0.3.2
> factory_girl_rails 1.0
>
> And the following for shared examples:
>
> module ExampleGroupMethods
>
>   describe "an admin is logged in", :shared => true do
>     before(:each) do
>       controller.stubs(:logged_in? => true)
>       controller.stubs(:current_user => Factory.create(:admin))
>     end
>   end
>
>   describe "a supporter is logged in", :shared => true do
>     before(:each) do
>       controller.stubs(:logged_in? => true)
>       controller.stubs(:current_user => Factory.create(:supporter))
>     end
>   end
>
>   describe "a manager is logged in", :shared => true do
>     before(:each) do
>       controller.stubs(:logged_in? => true)
>       controller.stubs(:current_user => Factory.create(:manager))
>     end
>   end
>
> end
>
> And the follow controller spec:
>
> describe UsersController do
>
>   it_should_behave_like "an admin is logged in"
>
>   describe "handling /users" do
>
>     before do
>       @user = mock_model(User)
>     end
>
>     def do_get
>       get :index
>     end
>
>     it "should assign users to the view" do
>       User.should_receive(:all).with(:order => "last_name,
> first_name").and_return([ @user ])
>       do_get
>     end
>
>   end
>
> end
>
> And finally this spec_helper:
>
> # This file is copied to spec/ when you run 'rails generate rspec:install'
> ENV["RAILS_ENV"] ||= 'test'
> require File.expand_path("../../config/environment", __FILE__)
> require 'rspec/rails'
>
> # Requires supporting ruby files with custom matchers and macros, etc,
> # in spec/support/ and its subdirectories.
> Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
>
> RSpec.configure do |config|
>   # == Mock Framework
>   #
>   # If you prefer to use mocha, flexmock or RR, uncomment the appropriate
> line:
>   #
>   # config.mock_with :mocha
>   # config.mock_with :flexmock
>   # config.mock_with :rr
>   config.mock_with :rspec
>
>   # Remove this line if you're not using ActiveRecord or ActiveRecord
> fixtures
>   # config.fixture_path = "#{::Rails.root}/spec/fixtures"
>
>   # If you're not using ActiveRecord, or you'd prefer not to run each of
> your
>   # examples within a transaction, remove the following line or assign false
>   # instead of true.
>   config.use_transactional_fixtures = true
>
>   # seehttp://asciicasts.com/episodes/157-rspec-matchers-macros
>   config.include(ControllerMacros, :type => :controller)
>
>   # seehttp://pastie.org/870928
>   config.include(ExampleGroupMethods, :type => :controller)
> end
>
> Anyone have a suggestion?  Thanks in advance
>
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users

Hello,

You actually have to use the `shared_examples_for` method. So, your
code should look like this:

shared_examples_for "an admin is logged in" do
  describe "an admin is logged in", :shared => true do
  .....


More information about the rspec-users mailing list