为什么我在黄瓜测试用例中得到未定义的局部变量或方法“角色”

时间:2013-10-11 11:32:20

标签: rspec ruby-on-rails-3.2 devise cancan rolify

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'

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'     用户工厂   

          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

我已经尝试过这么多来解决我自己的问题,我也用Google搜索了但是我无法解决它,&amp;我也是铁杆上的红宝石以及黄瓜的新手。如果我错了,请指导我,我的帮助将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

如果仔细观察堆栈跟踪:

   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'

即使执行了undefined local variable or method 'role'的方法,您也会发现错误消息./features/step_definitions/event_steps.rb:10以及引发错误create_visitor的源代码的位置。

查看您发布的来源时:

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

您可以看到您正在呼叫role。从您发布的代码中,没有定义role的位置。必须有一个变量或具有该名称的方法。也许这只是一个错字,你的意思是一个实例变量@role

我们可以为您提供更多帮助,这些都在您的代码中......