diff --git a/.rubocop.yml b/.rubocop.yml index f3ed8bc..58ef6e4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,6 +7,7 @@ AllCops: - '**/templates/**/*' - '**/vendor/**/*' - 'actionpack/lib/action_dispatch/journey/parser.rb' + - 'tmp/**/*' # Prefer &&/|| over and/or. Style/AndOr: diff --git a/.yardopts b/.yardopts new file mode 100644 index 0000000..25ec386 --- /dev/null +++ b/.yardopts @@ -0,0 +1,4 @@ +--exclude /templates/ +--quiet +act*/lib/**/*.rb +railties/lib/**/*.rb diff --git a/Gemfile b/Gemfile index 9d4fcc1..00a4a6d 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ source "https://rubygems.org" # Specify your gem's dependencies in action-cable-testing.gemspec gemspec -gem "actioncable", "~> 5.1" +gem "rails", "~> 5.1" gem "pry-byebug" diff --git a/README.md b/README.md index 0f2ad5d..5e18a20 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,15 @@ class ChatChannelTest < ActionCable::Channel::TestCase end ``` +### Generators + +This gem also provides Rails generators: + +```sh +# Generate a channel test case for ChatChannel +rails generate test_unit:channel chat +``` + ## Development After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake` to run the tests. diff --git a/Rakefile b/Rakefile index cad44f5..c2640b4 100644 --- a/Rakefile +++ b/Rakefile @@ -12,4 +12,4 @@ end RuboCop::RakeTask.new RSpec::Core::RakeTask.new(:spec) -task default: [:spec, :test, :rubocop] +task default: [:rubocop, :spec, :test] diff --git a/action-cable-testing.gemspec b/action-cable-testing.gemspec index c4ca967..9d1c753 100644 --- a/action-cable-testing.gemspec +++ b/action-cable-testing.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.license = "MIT" spec.files = `git ls-files`.split($/).select { |p| p.match(%r{^lib/}) } + - %w(README.md CHANGELOG.md LICENSE.txt) + %w(README.md CHANGELOG.md LICENSE.txt .yardopts) spec.require_paths = ["lib"] @@ -28,5 +28,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.5" spec.add_development_dependency "minitest", "~> 5.9" + spec.add_development_dependency "ammeter", "~> 1.1" spec.add_development_dependency "rubocop", "~> 0.51" end diff --git a/gemfiles/rails50.gemfile b/gemfiles/rails50.gemfile index 2b96f66..adcd94e 100644 --- a/gemfiles/rails50.gemfile +++ b/gemfiles/rails50.gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -gem "actioncable", "~> 5.0.0" +gem "rails", "~> 5.0.0" gemspec path: '..' diff --git a/lib/generators/test_unit/channel/channel_generator.rb b/lib/generators/test_unit/channel/channel_generator.rb new file mode 100644 index 0000000..fad93f2 --- /dev/null +++ b/lib/generators/test_unit/channel/channel_generator.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require "rails/generators/test_unit" + +module TestUnit # :nodoc: + module Generators # :nodoc: + class ChannelGenerator < Base # :nodoc: + source_root File.expand_path("../templates", __FILE__) + + check_class_collision suffix: "ChannelTest" + + def create_test_file + template "unit_test.rb.erb", File.join("test/channels", class_path, "#{file_name}_channel_test.rb") + end + end + end +end diff --git a/lib/generators/test_unit/channel/templates/unit_test.rb.erb b/lib/generators/test_unit/channel/templates/unit_test.rb.erb new file mode 100644 index 0000000..87a3515 --- /dev/null +++ b/lib/generators/test_unit/channel/templates/unit_test.rb.erb @@ -0,0 +1,9 @@ +require 'test_helper' + +<% module_namespacing do -%> +class <%= class_name %>ChannelTest < ActionCable::TestCase + # test "the truth" do + # assert true + # end +end +<% end -%> diff --git a/spec/generators/cable_spec.rb b/spec/generators/cable_spec.rb new file mode 100644 index 0000000..63ec64f --- /dev/null +++ b/spec/generators/cable_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "spec_helper" +require "generators/test_unit/channel/channel_generator" + +describe TestUnit::Generators::ChannelGenerator, type: :generator do + destination File.expand_path("../../../tmp", __FILE__) + + let(:args) { ["chat"] } + + before do + prepare_destination + run_generator(args) + end + + subject { file("test/channels/chat_channel_test.rb") } + + it "creates script", :aggregate_failures do + is_expected.to exist + is_expected.to contain("class ChatChannelTest < ActionCable::TestCase") + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..0969569 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) + +require "pry-byebug" + +require "action_controller/railtie" +require "action_view/railtie" +require "action_cable" +require "action-cable-testing" + +require "ammeter/init" + +# Require all the stubs and models +Dir[File.expand_path("../test/stubs/*.rb", __dir__)].each { |file| require file } + +# # Set test adapter and logger +ActionCable.server.config.cable = { "adapter" => "test" } +ActionCable.server.config.logger = + ActiveSupport::TaggedLogging.new ActiveSupport::Logger.new(StringIO.new) + +Dir[File.expand_path("support/**/*.rb", __dir__)].each { |f| require f } + +RSpec.configure do |config| + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.example_status_persistence_file_path = "tmp/rspec_examples.txt" + config.filter_run :focus + config.run_all_when_everything_filtered = true + + config.order = :random + Kernel.srand config.seed +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 7748b8c..902e968 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,24 +16,3 @@ ActionCable.server.config.cable = { "adapter" => "test" } ActionCable.server.config.logger = ActiveSupport::TaggedLogging.new ActiveSupport::Logger.new(StringIO.new) - -class ActionCable::TestCase < ActiveSupport::TestCase - def wait_for_async - wait_for_executor Concurrent.global_io_executor - end - - def run_in_eventmachine - yield - wait_for_async - end - - def wait_for_executor(executor) - # do not wait forever, wait 2s - timeout = 2 - until executor.completed_task_count == executor.scheduled_task_count - sleep 0.1 - timeout -= 0.1 - raise "Executor could not complete all tasks in 2 seconds" unless timeout > 0 - end - end -end