Reputation: 331
My Ruby on Rails application is working as desired. Things are getting broadcast and received as I want. However, I want to add unit testing to channels using action-cable-testing
gem. My user registration is done using Devise
.
Here is my connection.rb
file:
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
I have a message_notifications_channel.rb
like this:
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
This channel allows a user, when signed in, to stream from message_notifications_channel_accountID
where accountID is id of the account to which the user belongs.
My cable.js
file is the same as shown in the ruby guide:
// 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);
I'm having trouble with the following rspec test:
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
The error I got is: Failure/Error: if current_user&.account_id
NameError:
undefined local variable or method `current_user' for #<MessageNotificationsChannel:0x000000000721d890>
The location of the error is in my message_notifications_channel.rb
file. I put a byebug
before self.current_user = find_verified_user
in the connect
method in connection.rb
, the test would not hit that byebug at all. And no surprise, I would get that error later.
When I'm running in development environment that byebug will get hit and things will run normally.
This is my cable.yml
:
redis: &redis
adapter: redis
url: redis://localhost:6379/1
production: *redis
development: *redis
test:
*redis
I've looked at https://github.com/palkan/action-cable-testing/issues/26#issuecomment-392481619 That person's code does not really use Devise. It's crucial that all parts of my app uses devise for user authentication. Thanks!
Upvotes: 4
Views: 1107
Reputation: 26
Try to add "stub_connection current_user: users(:some_user_from_fixtures)" to your test. So for your code it would be:
it 'accepts connection' do
sign_in user
stub_connection current_user: user
subscribe(account_id: user.account_id)
end
This should work. For more information visit rails official documentation: Testing Action Cable
Upvotes: 1