2

我正在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

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

有人能指出我正确的方向吗?

4

3 回答 3

2

对于 Refinery 2,所需要的只是 spec/support/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.rbhttps://github.com的其他助手一起可用 /resolve/refinerycms/blob/master/testing/lib/refinery/testing/request_macros/authentication.rb

于 2012-08-12T23:57:25.460 回答
1

这就是我使用 Refinery 1.0.9 的方式,可能需要针对 Refinery 2 进行调整

创建一个名为“controller_macros.rb”的文件并将其放在 spec/support/refinery/ 下

在 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

在您的 factory.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 文件中,将控制器宏模块包含到您的控制器测试中,并确保您有 Devise 测试助手

RSpec.configure do |config|
  config.include ::Devise::TestHelpers, :type => :controller
  config.extend  ::Refinery::ControllerMacros, :type => :controller
end
于 2012-05-29T16:16:43.750 回答
0

帮助我的是确保您将插件/引擎名称添加到当前登录用户允许访问的插件列表中:

refinery_login_with :refinery_user 

before do
  logged_in_user.plugins = logged_in_user.plugins | %w(<plugin name>)
end
于 2015-07-15T12:23:23.527 回答