如何登录在RefineryCMS测试用户?(How to login a user in Refin

2019-09-17 08:36发布

我正在开发中refineryCMS定制控制器,即放置在应用程序的根

class ExchangeController < ApplicationController
    def exchange
    end
end

它工作正常,但有它测试我需要登录炼油厂用户喜欢这个

describe "Exchange action" do
   login_refinery_user
   it 'should return a 200' do
    get '/exchange' 
    response.code.should == '200'
   end
end

但它只是不'没有调用该方法登录即时得到找不到表refinery_roles‘工作:((未定义的局部变量或方法`login_refinery_user)’

可能有人点我在正确的方向?

Answer 1:

对于炼油厂2,所有的需要都在规格/支持/ devise.rb下面几行:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
  config.extend  ::Refinery::Testing::ControllerMacros::Authentication, :type => :controller
  config.extend  ::Refinery::Testing::RequestMacros::Authentication, :type => :request

end

现在,“login_refinery_user”将与来自其他助手一起提供https://github.com/resolve/refinerycms/blob/master/testing/lib/refinery/testing/controller_macros/authentication.rb和https://github.com /resolve/refinerycms/blob/master/testing/lib/refinery/testing/request_macros/authentication.rb



Answer 2:

这就是我如何与炼油厂1.0.9这样做,它可能需要调整了炼油厂2

创建一个名为“controller_macros.rb”的文件,并将其放置在规格/支持/炼油厂/

在controller_macros.rb定义login_refinery_user

module Refinery
  module ControllerMacros
    def login_refinery_user
      before (:each) do
        @refinery_user = Factory(:refinery_user)
        @request.env["devise.mapping"] = Devise.mappings[:admin]
        sign_in @refinery_user
      end
    end
  end
end

在你factories.rb文件请确保您有以下两个工厂

factory :user do
  sequence(:username) { |n| "person#{n}" }
  sequence(:email) { |n| "person#{n}@cucumber.com" }
  password  "greenandjuicy"
  password_confirmation "greenandjuicy"
end

factory :refinery_user, :parent => :user do
  roles { [ Role[:refinery] ] }

  after_create do |user|
    Refinery::Plugins.registered.each_with_index do |plugin, index|
      user.plugins.create(:name => plugin.name, :position => index)
    end
  end
end

在您的spec_helper.rb文件包括控制器模块宏到你的控制器测试,还请确保您有在设计测试助手

RSpec.configure do |config|
  config.include ::Devise::TestHelpers, :type => :controller
  config.extend  ::Refinery::ControllerMacros, :type => :controller
end


Answer 3:

什么帮助我是要确保你把你的插件/引擎名称插件当前登录的用户列表中被允许访问:

refinery_login_with :refinery_user 

before do
  logged_in_user.plugins = logged_in_user.plugins | %w(<plugin name>)
end


文章来源: How to login a user in RefineryCMS tests?