ruby-on-rails - 使用 Devise 进行动作电缆测试
问题描述
我的 Ruby on Rails 应用程序可以正常工作。事情正在按我的意愿广播和接收。action-cable-testing
但是,我想使用gem向通道添加单元测试。我的用户注册是使用Devise
. 这是我的connection.rb
文件:
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
reject_unauthorized_connection
end
end
end
end
我有一个message_notifications_channel.rb
这样的:
class MessageNotificationsChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
if current_user&.account_id
stream_from "message_notifications_channel_#{current_user.account_id}"
end
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
此频道允许用户在登录时从message_notifications_channel_accountID
其中 accountID 是用户所属帐户的 ID 进行流式传输。
我的cable.js
文件与 ruby 指南中显示的相同:
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
我在使用以下 rspec 测试时遇到问题:
require 'rails_helper'
RSpec.describe MessageNotificationsChannel, type: :channel do
let(:authorized_senders) {['11111']}
let(:common_user_phone){'17777777777'}
let(:account) {FactoryGirl.create(:account, authorized_senders: authorized_senders)}
let(:user){FactoryGirl.create(:user, account: account, admin: true)}
context 'when user is authenticated' do
describe '#connect' do
it 'accepts connection' do
sign_in user
subscribe(account_id: user.account_id)
end
end
end
end
我得到的错误是:Failure/Error: if current_user&.account_id
NameError:
undefined local variable or method `current_user' for #<MessageNotificationsChannel:0x000000000721d890>
错误的位置在我的message_notifications_channel.rb
文件中。我在方法中放了一个byebug
before ,测试根本不会碰到那个 byebug。毫不奇怪,我稍后会收到该错误。self.current_user = find_verified_user
connect
connection.rb
当我在开发环境中运行时,byebug 会被击中并且事情会正常运行。
这是我的cable.yml
:
redis: &redis
adapter: redis
url: redis://localhost:6379/1
production: *redis
development: *redis
test:
*redis
我查看了https://github.com/palkan/action-cable-testing/issues/26#issuecomment-392481619 该人的代码并没有真正使用设计。我的应用程序的所有部分都使用设计进行用户身份验证,这一点至关重要。谢谢!
解决方案
尝试将“ stub_connection current_user: users(:some_user_from_fixtures) ”添加到您的测试中。因此,对于您的代码,它将是:
it 'accepts connection' do
sign_in user
stub_connection current_user: user
subscribe(account_id: user.account_id)
end
这应该有效。更多信息请访问 rails 官方文档:Testing Action Cable
推荐阅读
- html - 引导模式未显示在操作中
- standardjs - 找不到模块“babel-eslint”(标准和 babel-eslint 已在本地安装)
- delphi - 如何获取 TPersistentClass 来自的动态包?
- c++ - 反转字符串?更优化的方式
- xml - 行错误:76 col:82 Line:76 ';' 预期,得到 char[61]
- push - ejabberd mod_push 和 mod_push_keepalive 不启动
- sql - 如何使用 sql server 根据上一行和下一行显示记录
- reactjs - React componentDid mount 获取异步图像。如果在更改页面之前没有完成,如何取消请求?
- codenameone - CN1:带 Alpha 通道的渐变
- assembly - 在运行时中断汇编指令