-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ActionCable::Channel::TestCase #33969
Add ActionCable::Channel::TestCase #33969
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic, @palkan. Would you add a changelog entry to match?
class NonInferrableChannelError < ::StandardError | ||
def initialize(name) | ||
super "Unable to determine the channel to test from #{name}. " + | ||
"You'll need to specify it using tests YourChannel in your " + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backticks around "tests YourChannel" to be clear that it's code to be added to the test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Done
def test_set_channel_class_manual_using_string | ||
assert_equal TestTestChannel, self.class.channel_class | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use more precise language for these, e.g. Crazy → NonInferrable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stole this craziness from action_mailer tests 🙂
Fixed
perform :broadcast_to_user, message: "SOS" | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great test coverage and demonstration of the test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
ActionCable::Channel::TestCase provides an ability to unit-test channel classes. There are several reasons to write unit/functional cable tests: - Access control (who has access to the channel? who can perform action and with which argument? - Frontend-less applications have no system tests at all–and we still need a way to test channels logic. See also rails#27191
2a8f92e
to
16e821a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good re. changelog!
@subscription.singleton_class.include(ChannelStub) | ||
@subscription.send(:initialize, connection, CHANNEL_IDENTIFIER, params.with_indifferent_access) | ||
# Call subscribe_to_channel if it's public (Rails 5.0.1+) | ||
@subscription.subscribe_to_channel if ActionCable.gem_version >= Gem::Version.new("5.0.1") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this check still necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! No, we don't need it in Rails 6.
Will send a follow-up.
Thanks for catching!
Follow-up #33659.
ActionCable::Channel::TestCase provides an ability
to unit-test channel classes.
From #23211 (comment):
See previous discussion here #27191
Summary
Channels Testing
Channels tests are written as follows:
subscribe
method to simulate subscription creation.transmitted messages, subscribed streams, etc.
For example:
You can also perform actions:
You can set up your connection identifiers:
When broadcasting to an object: