Question

following error i am getting

   And I am exists as a parent      # features/step_definitions/kid_steps.rb:106
   undefined local variable or method `role' for #<Cucumber::Rails::World:0xc4722f8> (NameError)
  ./features/step_definitions/event_steps.rb:10:in `create_visitor'
  ./features/step_definitions/event_steps.rb:14:in `create_user'
  ./features/step_definitions/kid_steps.rb:107:in `/^I am exists as a parent$/'
  features/manage_kids.feature:11:in `And I am exists as a parent'

user factories

          FactoryGirl.define do

           factory :user do
             email "user@example.com"
             password "test123"
             password_confirmation "test123"
          end
          factory :role do
             id "1"
             name "admin"

             id "2" 
             name "parent"

             id "3" 
             name "gifter"
          end
       end </code>

user.rb

    def role?(role)
       return !!self.roles.find_by_name(role.to_s)
    end

ability.rb

   class Ability
     include CanCan::Ability

     def initialize(user)

      user ||= User.new #user

        entities = [Kid, Customer, Event, Contact]

         # check if user is 'admin' grant all permissions
       if user.role? :admin
        can :manage, :all
      else
       can :manage, entities
      end

    end
  end

#event_steps.rb

       def create_visitor

             @visitor ||= { :email => "user@example.com",
             :password => "test123", 
             :password_confirmation => "test123", 
             :role => Role.find_by_name(role.to_s)}
      end

I have tried so much to resolve problem by my own along with i have googled also but i couldn't able to solve it,& i am new to ruby on rails as well as cucumber also.Please guide me if i am wrong ,your help will be appreciated. Thank you

Was it helpful?

Solution

If you look at the stacktrace carefully:

   And I am exists as a parent      # features/step_definitions/kid_steps.rb:106
   undefined local variable or method `role' for #<Cucumber::Rails::World:0xc4722f8> (NameError)
  ./features/step_definitions/event_steps.rb:10:in `create_visitor'
  ./features/step_definitions/event_steps.rb:14:in `create_user'
  ./features/step_definitions/kid_steps.rb:107:in `/^I am exists as a parent$/'
  features/manage_kids.feature:11:in `And I am exists as a parent'

you will find the error message undefined local variable or method 'role' and the location of the source code that is raising the error ./features/step_definitions/event_steps.rb:10 even with the method that is executed create_visitor.

When looking at the source that you posted:

def create_visitor
  @visitor ||= { :email => "user@example.com",
    :password => "test123", 
    :password_confirmation => "test123", 
    :role => Role.find_by_name(role.to_s)} # i assume that this is line 10!
end

you can see that you are calling role. From the code that you post, there is no point where role is defined. There has to be a variable or a method with that name. Maybe it's just a typo and you mean an instance variable @role.

there is nothing more that we can help you, it's all in your code...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top