Scope problems for before hooks using helper methods?

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:inblock 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:inload’
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:inmap’
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:inrun’
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:inrun’
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)

On Oct 13, 2011, at 7:35 PM, Gordon wrote:

a) not signed in
It fails and I suspect that the current scope within the before hook
from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
rspec/core/command_line.rb:18:in `run’

Brand. As you add validations to Brand, be sure to

   describe 'brands access is not available to users who have not

       it 'admin users that are logged in can access the index
       login_admin_user

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

What am I doing wrong?


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Let’s see the helper method please.

Let’s see the helper method please.

---------------spec/support/controller_macros.rb: start ----------

module ControllerMacros
include Devise::TestHelpers

sets up an instance of a non-admin user

def login_user
before(:each) do
@request.env[“devise.mapping”] = Devise.mappings[:user]
@user = FactoryGirl.create(:user)
sign_in @user
end
end

sets up an instance of a admin user

def login_admin_user
before(:each) do
@request.env[“devise.mapping”] = Devise.mappings[:user]
@admin_user = FactoryGirl.create(:admin)
sign_in @admin_user
end
end
end

---------------spec/support/controller_macros.rb: end ----------

It works for my existing controller specs and it’s based on
https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec)

On Oct 13, 2011, at 8:35 PM, Gordon wrote:

Hi, there,

I believe I have some problem with scoping.

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)

This is a syntax error. Please fix that first, and then we can talk
about scoping :slight_smile:

Cheers,
David

On Oct 14, 2011, at 12:01 AM, Gordon wrote:

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

login_admin_user creates a before hook, but here it’s in a before
hook
. That doesn’t work. Try:

context ‘with an admin user signed in’ do
login_admin_user # not inside a before hook
it ‘…’ do
# …
end
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

HTH,
David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs