mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add channel test generator
This commit is contained in:
parent
3631d7eee4
commit
0f41aa30d3
5 changed files with 48 additions and 1 deletions
|
@ -7,6 +7,7 @@ Example:
|
|||
========
|
||||
rails generate channel Chat speak
|
||||
|
||||
creates a Chat channel class and JavaScript asset:
|
||||
creates a Chat channel class, test and JavaScript asset:
|
||||
Channel: app/channels/chat_channel.rb
|
||||
Test: test/channels/chat_channel_test.rb
|
||||
Assets: app/javascript/channels/chat_channel.js
|
||||
|
|
|
@ -11,6 +11,8 @@ module Rails
|
|||
|
||||
check_class_collision suffix: "Channel"
|
||||
|
||||
hook_for :test_framework
|
||||
|
||||
def create_channel_file
|
||||
template "channel.rb", File.join("app/channels", class_path, "#{file_name}_channel.rb")
|
||||
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module TestUnit
|
||||
module Generators
|
||||
class ChannelGenerator < ::Rails::Generators::NamedBase
|
||||
source_root File.expand_path("templates", __dir__)
|
||||
|
||||
check_class_collision suffix: "ChannelTest"
|
||||
|
||||
def create_test_files
|
||||
template "channel_test.rb", File.join("test/channels", class_path, "#{file_name}_channel_test.rb")
|
||||
end
|
||||
|
||||
private
|
||||
def file_name # :doc:
|
||||
@_file_name ||= super.sub(/_channel\z/i, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class <%= class_name %>ChannelTest < ActionCable::Channel::TestCase
|
||||
# test "subscribes" do
|
||||
# subscribe
|
||||
# assert subscription.confirmed?
|
||||
# end
|
||||
end
|
|
@ -67,12 +67,23 @@ class ChannelGeneratorTest < Rails::Generators::TestCase
|
|||
assert_file "app/javascript/channels/consumer.js"
|
||||
end
|
||||
|
||||
def test_invokes_default_test_framework
|
||||
run_generator %w(chat -t=test_unit)
|
||||
|
||||
assert_file "test/channels/chat_channel_test.rb" do |test|
|
||||
assert_match(/class ChatChannelTest < ActionCable::Channel::TestCase/, test)
|
||||
assert_match(/# test "subscribes" do/, test)
|
||||
assert_match(/# assert subscription.confirmed\?/, test)
|
||||
end
|
||||
end
|
||||
|
||||
def test_channel_on_revoke
|
||||
run_generator ["chat"]
|
||||
run_generator ["chat"], behavior: :revoke
|
||||
|
||||
assert_no_file "app/channels/chat_channel.rb"
|
||||
assert_no_file "app/javascript/channels/chat_channel.js"
|
||||
assert_no_file "test/channels/chat_channel_test.rb"
|
||||
|
||||
assert_file "app/channels/application_cable/channel.rb"
|
||||
assert_file "app/channels/application_cable/connection.rb"
|
||||
|
@ -88,5 +99,8 @@ class ChannelGeneratorTest < Rails::Generators::TestCase
|
|||
|
||||
assert_no_file "app/javascript/channels/chat_channel_channel.js"
|
||||
assert_file "app/javascript/channels/chat_channel.js"
|
||||
|
||||
assert_no_file "test/channels/chat_channel_channel_test.rb"
|
||||
assert_file "test/channels/chat_channel_test.rb"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue