【发布时间】:2016-06-30 18:23:57
【问题描述】:
我有这个代码来验证频道订阅者:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
protected
def find_verified_user
if current_user = User.find_by(id: cookies.signed[:user_id])
current_user
else
reject_unauthorized_connection
end
end
end
end
一切正常。问题在于功能测试。 当我运行这个测试时:
require 'rails_helper'
feature 'Chat room' do
scenario "send one message" do
user = create(:user)
login_as(user, :scope => :user)
expect {
fill_in 'message', with: 'hello friend'
click_button 'Send'
byebug
}.to change(Message, :count).by(1)
expect(current_path).to eq root_path
expect(page).to have_content 'hello friend'
end
end
测试日志显示“拒绝了未经授权的连接尝试”。由于cookie是空的,所以无法认证。
那么如何在 capybara 测试中设置 cookie?
我在测试中尝试这样做cookies.signed[:user_id] = user.id,但它不起作用。
如何在测试中设置像 cookies.signed[:user_id] = user.id 这样的 cookie?
【问题讨论】:
-
您对
login_as的定义是什么?您实际访问页面的时间/地点是什么?
标签: ruby-on-rails cookies rspec capybara actioncable