【问题标题】:action-cable-testing with Devise使用 Devise 进行动作电缆测试
【发布时间】:2019-03-04 19:23:30
【问题描述】:

我的 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

我得到的错误是: 失败/错误:如果 current_user&.account_id

 NameError:
   undefined local variable or method `current_user' for #<MessageNotificationsChannel:0x000000000721d890>

错误的位置在我的message_notifications_channel.rb 文件中。我在connection.rbconnect 方法中的self.current_user = find_verified_user 之前放了一个byebug,测试根本不会碰到那个byebug。毫不奇怪,我稍后会收到该错误。

当我在开发环境中运行时,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 那个人的代码并没有真正使用 Devise。我的应用程序的所有部分都使用设计进行用户身份验证,这一点至关重要。 谢谢!

【问题讨论】:

    标签: ruby-on-rails rspec devise channel actioncable


    【解决方案1】:

    尝试将“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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 2018-07-29
      相关资源
      最近更新 更多