[rspec-users] Scope problems for before hooks using helper methods?

Gordon anexiole at gmail.com
Thu Oct 13 21:35:39 EDT 2011



Hi, there,


 I believe I have some problem with scoping.

  I have a controller spec file which tests a brands resource.
 At the start of the file, I test the resource's access for different
users in a context block
a) not signed in
b) non-admin user signed in- I call my own helper method, login_user
c) admin user signed in - I call my own helper method,
login_admin_user
Specs there do pass successfully.

I then create another context block to just test the resource for an
admin user that's signed in.
I tried calling login_admin_user in a before hook as per the previous
specs .

It fails and I suspect that the current scope within the before hook
does not see my custom helper method, login_admin_user.
 Here is the error message:
--------- Extract start -----------------

/usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `load': /Users/anexiole/projects/
try_rails/spec/controllers/brands_controller_spec.rb:164: syntax
error, unexpected keyword_end, expecting $end (SyntaxError)
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `block in load'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:227:in `load_dependency'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `load'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `block in load_spec_files'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `map'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `load_spec_files'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/command_line.rb:18:in `run'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:80:in `run_in_process'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:69:in `run'
	from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:11:in `block in autorun'
---------- Extract end ------------------


My specs is as follows:

--------- spec/controllers/brands_controller_spec.rb (start)
--------------


require 'spec_helper'

describe BrandsController do

  # This should return the minimal set of attributes required to
create a valid
  # Brand. As you add validations to Brand, be sure to
  # update the return value of this method accordingly.
  def valid_attributes
    {
        'name' => 'J Speed',
        'description' => 'From gunsai province'
    }
  end


    context 'checking access for varying users' do
        describe 'brands access is not available to users who have not
signed in' do
            it 'users that are not logged in will be sent to the sign
in page' do
                get :index
                response.should redirect_to(new_user_session_path)
            end
        end

        describe 'brands access is not available to regular users' do
            login_user

            it 'regular users that are logged in will be sent to home
page' do
                get :index
                response.should redirect_to(root_path)
            end
        end

        describe 'brands access is available only to admin users' do
            login_admin_user

            it 'admin users that are logged in can access the index
page' do
                get :index
                response.should render_template('index')
            end
        end
    end

    context 'with an admin user signed in' do    # <----- starts
failing in this context
        before(:each) do
            login_admin_user
        end

        describe "creates a new brand entry" do
            it "assigns a new brand as @brand" do
                get :new
                assigns(:brand).should be_a_new(Brand)
            end
        end
    end
end

--------- spec/controllers/brands_controller_spec.rb (end)
--------------

What am I doing wrong?


More information about the rspec-users mailing list