Skip to content

Commit

Permalink
Add test unit generator
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 22, 2017
1 parent bc0beb2 commit 4a78615
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 25 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ AllCops:
- '**/templates/**/*'
- '**/vendor/**/*'
- 'actionpack/lib/action_dispatch/journey/parser.rb'
- 'tmp/**/*'

# Prefer &&/|| over and/or.
Style/AndOr:
Expand Down
4 changes: 4 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--exclude /templates/
--quiet
act*/lib/**/*.rb
railties/lib/**/*.rb
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ end
RuboCop::RakeTask.new
RSpec::Core::RakeTask.new(:spec)

task default: [:spec, :test, :rubocop]
task default: [:rubocop, :spec, :test]
3 changes: 2 additions & 1 deletion action-cable-testing.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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
2 changes: 1 addition & 1 deletion gemfiles/rails50.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'

gem "actioncable", "~> 5.0.0"
gem "rails", "~> 5.0.0"

gemspec path: '..'
17 changes: 17 additions & 0 deletions lib/generators/test_unit/channel/channel_generator.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions lib/generators/test_unit/channel/templates/unit_test.rb.erb
Original file line number Diff line number Diff line change
@@ -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 -%>
22 changes: 22 additions & 0 deletions spec/generators/cable_spec.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 0 additions & 21 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4a78615

Please sign in to comment.