使用RSpec测试自定义设备会话控制器

时间:2015-07-21 16:52:36

标签: ruby-on-rails-4 rspec devise rspec-rails

我使用自定义Devise SessionsController:

#routes.rb
devise_for :custom_users, { 
    :singular => 'custom_user',
    :class_name => 'CustomUser', 
    :path => "/", 
    :path_names => { :sign_in => 'login', :sign_out => 'logout' },
    :controllers => { :sessions => "custom_sessions" } 
}

在我的routes.rb设计中设置如下:

#custom_sessions_controller_spec.rb
require 'rails_helper'
describe CustomSessionsController, :type => :controller do

  describe "login" do
    before do
      setup_controller_for_warden
      @request.env["devise.mapping"] = Devise.mappings[:custom_user]
      @my_user = FactoryGirl.create(:custom_user) # creates user with valid credentials
    end

    it "should succeed with valid credentials" do
      sign_in @my_user
      curr_user = assigns(:current_custom_user)

      expect(curr_user).to eq(@my_user)
      expect(response).to be_success
    end
  end
end

我想写一个简单的rspec测试:

rails_helper.rb

在我的require 'devise' ... RSpec.configure do |config| ... config.include Devise::TestHelpers, :type => :controller config.include Warden::Test::Helpers , :type => :controller ... 中,存在以下行:

curr_user

问题是测试中的nil始终是Gtk: cannot open display。我究竟做错了什么?或者如何测试自定义设备会话控制器?或者如何使用自定义设计会话登录 - 其他 - 测试?

2 个答案:

答案 0 :(得分:2)

而不是:

  

描述CustomSessionsController,:type => :控制器做

尝试:

  

RSpec.describe CustomSessionsController,:type => :控制器做

希望它有效!

答案 1 :(得分:0)

使用

@controller.current_custom_user

而不是

assigns(:current_custom_user)

似乎有效。