【发布时间】:2018-12-16 07:52:32
【问题描述】:
导轨 5.2 我有以下 ApplicationCable::Connection ruby 文件:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
if verified_user = env['warden'].user
verified_user
else
message = "The user is not found. Connection rejected."
logger.add_tags 'ActionCable', message
self.transmit error: message
reject_unauthorized_connection
end
end
end
end
我想测试此设置并使用以下 RSpec 测试:
require 'rails_helper.rb'
RSpec.describe ApplicationCable::Connection, type: :channel do
it "successfully connects" do
connect "/cable", headers: { "X-USER-ID" => 325 }
expect(connection.user_id).to eq 325
end
end
失败的原因是:
失败/错误:如果verified_user = env['warden'].user
无方法错误: nil:NilClass 的未定义方法“[]”
所以我想删除 env['warden'].user 代码并返回 325 的 id。 我尝试了以下方法:
allow(env['warden']).to receive(:user).and_return(325)
但这产生了以下错误:
undefined local variable or methodenv'
如何测试这个类?
【问题讨论】:
标签: rspec devise rspec-rails actioncable