Skip to content

Using ActionCable::Channel::TestCase causes NameError: TestServer is missing #47377

@yykamei

Description

@yykamei

I came across an error NameError: uninitialized constant ActionCable::Channel::ConnectionStub::TestServer when I run tests with action_cable.

#47300 added @server in ActionCable::Channel::ConnectionStub as an instance of TestServer, and TestServer seems to be located in actioncable/test. Perhaps, Rails testing scripts usually load test helpers, so the problem hasn't occurred. However, such an implicit loading might cause unstable test behavior, so I think TestServer should be loaded when requiring ActionCable::Channel::TestCase. But I'm not sure how to do that. TestServer should be written inside actioncable/lib? That's why I opened an issue here.

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source 'https://rubygems.org'

  gem "rails", github: "rails/rails", ref: "d6eec533c186c92448e7413e9af9c2a2009aca99"
end

require "action_cable"
require "minitest/autorun"
require "logger"

ActionCable.server.config.cable ||= {}
ActionCable.server.config.cable["adapter"] = "async"
ActionCable.server.config.logger = Logger.new(STDOUT)

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end

module ApplicationCable
  class Channel < ActionCable::Channel::Base
    def hello(data)
      ActionCable.server.broadcast("nice_#{params[:number]}", data)
    end

    private

    def subscribed
      stream_from "nice_#{params[:number]}"
      reject if Integer(params[:number], exception: false).negative?
    end
  end
end

class Test < ActionCable::Channel::TestCase
  tests ApplicationCable::Channel

  def test_hello
    subscribe number: "2"
    perform :hello, { name: "MM" }
  end
end

Expected behavior

I expect the test to pass in the above example without any exceptions.

Actual behavior

Error:
Test#test_hello:
NameError: uninitialized constant ActionCable::Channel::ConnectionStub::TestServer
    /Users/yutaka.kamei/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/bundler/gems/rails-d6eec533c186/actioncable/lib/action_cable/channel/test_case.rb:60:in `initialize'
    /Users/yutaka.kamei/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/bundler/gems/rails-d6eec533c186/actioncable/lib/action_cable/channel/test_case.rb:236:in `new'
    /Users/yutaka.kamei/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/bundler/gems/rails-d6eec533c186/actioncable/lib/action_cable/channel/test_case.rb:236:in `stub_connection'
    /Users/yutaka.kamei/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/bundler/gems/rails-d6eec533c186/actioncable/lib/action_cable/channel/test_case.rb:241:in `subscribe'
    a.rb:43:in `test_hello'

System configuration

Rails version:
7.1.0.alpha (d6eec53)

Ruby version:
3.2.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions