未定义的局部变量或方法'current_user'(rails)

时间:2012-06-13 08:44:57

标签: ruby-on-rails testing helper

我正在测试我的项目,它是关于包含此方法的application_helper.rb:

def delete_link table=""
        if current_user.role.name=="Admin" || current_user.role.name=="Pemilik"
            link_to 'Hapus', table, :confirm => 'Anda yakin?', :method => :delete
        end
    end

我想测试该方法,所以我在customers_controller_test.rb上进行了这样的测试

require 'test_helper'

class CustomersControllerTest < ActionController::TestCase
  include Devise::TestHelpers
  include ApplicationHelper
  setup do
    @permission = Permission.create(:name=>"Customer")
    @role = Role.create(:name=>"Admin")
    @user = User.create(:name=>"Admin", :role_id => 1, :email => "email@email.email", :password => "123456")
    @role_permission = RolePermission.create(:role_id=>@role.id, :permission_id=>@permission.id, :access=>2)
    @customer = FactoryGirl.create(:customer)

    sign_in @user    
  end

  test "should link to delete" do
    puts assert sign_in @user
    current_user = @user
    puts current_user.name
    puts current_user.role.name
    assert delete_link(customer_path(@customer.id))
    puts "2"
  end
end

但是我得到了Undefined局部变量或方法'current_user'。任何的想法?谢谢你的建议:)

1 个答案:

答案 0 :(得分:4)

设计的current_user方法在应用程序控制器上定义,因此在帮助程序中不可用。通过辅助方法的参数为用户提供并设置。