1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #21279 from ronakjangir47/test_cleanup

Cleaned up generators tests using internal assertion helper
This commit is contained in:
Kasper Timm Hansen 2015-08-20 07:37:59 +02:00
commit c83b117d4b
5 changed files with 30 additions and 45 deletions

View file

@ -1,11 +1,13 @@
require 'minitest/mock'
module ActiveSupport module ActiveSupport
module Testing module Testing
module MethodCallAssertions # :nodoc: module MethodCallAssertions # :nodoc:
private private
def assert_called(object, method_name, message = nil, times: 1) def assert_called(object, method_name, message = nil, times: 1, returns: nil)
times_called = 0 times_called = 0
object.stub(method_name, proc { times_called += 1 }) { yield } object.stub(method_name, proc { times_called += 1; returns }) { yield }
error = "Expected #{method_name} to be called #{times} times, " \ error = "Expected #{method_name} to be called #{times} times, " \
"but was called #{times_called} times" "but was called #{times_called} times"

View file

@ -33,6 +33,12 @@ class MethodCallAssertionsTest < ActiveSupport::TestCase
end end
end end
def test_assert_called_returns
assert_called(@object, :increment, returns: 10) do
assert_equal 10, @object.increment
end
end
def test_assert_called_failure def test_assert_called_failure
error = assert_raises(Minitest::Assertion) do error = assert_raises(Minitest::Assertion) do
assert_called(@object, :increment) do assert_called(@object, :increment) do

View file

@ -1,7 +1,6 @@
require 'generators/generators_test_helper' require 'generators/generators_test_helper'
require 'rails/generators/rails/app/app_generator' require 'rails/generators/rails/app/app_generator'
require 'env_helpers' require 'env_helpers'
require 'minitest/mock'
class ActionsTest < Rails::Generators::TestCase class ActionsTest < Rails::Generators::TestCase
include GeneratorsTestHelper include GeneratorsTestHelper
@ -12,13 +11,11 @@ class ActionsTest < Rails::Generators::TestCase
def setup def setup
Rails.application = TestApp::Application Rails.application = TestApp::Application
@mock_generator = Minitest::Mock.new
super super
end end
def teardown def teardown
Rails.application = TestApp::Application.instance Rails.application = TestApp::Application.instance
@mock_generator.verify
end end
def test_invoke_other_generator_with_shortcut def test_invoke_other_generator_with_shortcut
@ -150,16 +147,13 @@ class ActionsTest < Rails::Generators::TestCase
end end
def test_git_with_symbol_should_run_command_using_git_scm def test_git_with_symbol_should_run_command_using_git_scm
@mock_generator.expect(:call, nil, ['git init']) assert_called_with(generator, :run, ['git init']) do
generator.stub(:run, @mock_generator) do
action :git, :init action :git, :init
end end
end end
def test_git_with_hash_should_run_each_command_using_git_scm def test_git_with_hash_should_run_each_command_using_git_scm
@mock_generator.expect(:call, nil, ["git rm README"]) assert_called_with(generator, :run, [ ["git rm README"], ["git add ."] ]) do
@mock_generator.expect(:call, nil, ["git add ."])
generator.stub(:run, @mock_generator) do
action :git, rm: 'README', add: '.' action :git, rm: 'README', add: '.'
end end
end end
@ -185,15 +179,13 @@ class ActionsTest < Rails::Generators::TestCase
end end
def test_generate_should_run_script_generate_with_argument_and_options def test_generate_should_run_script_generate_with_argument_and_options
@mock_generator.expect(:call, nil, ['bin/rails generate model MyModel', verbose: false]) assert_called_with(generator, :run_ruby_script, ['bin/rails generate model MyModel', verbose: false]) do
generator.stub(:run_ruby_script, @mock_generator) do
action :generate, 'model', 'MyModel' action :generate, 'model', 'MyModel'
end end
end end
def test_rake_should_run_rake_command_with_default_env def test_rake_should_run_rake_command_with_default_env
@mock_generator.expect(:call, nil, ["rake log:clear RAILS_ENV=development", verbose: false]) assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false]) do
generator.stub(:run, @mock_generator) do
with_rails_env nil do with_rails_env nil do
action :rake, 'log:clear' action :rake, 'log:clear'
end end
@ -201,15 +193,13 @@ class ActionsTest < Rails::Generators::TestCase
end end
def test_rake_with_env_option_should_run_rake_command_in_env def test_rake_with_env_option_should_run_rake_command_in_env
@mock_generator.expect(:call, nil, ['rake log:clear RAILS_ENV=production', verbose: false]) assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
generator.stub(:run, @mock_generator) do
action :rake, 'log:clear', env: 'production' action :rake, 'log:clear', env: 'production'
end end
end end
def test_rake_with_rails_env_variable_should_run_rake_command_in_env def test_rake_with_rails_env_variable_should_run_rake_command_in_env
@mock_generator.expect(:call, nil, ['rake log:clear RAILS_ENV=production', verbose: false]) assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
generator.stub(:run, @mock_generator) do
with_rails_env "production" do with_rails_env "production" do
action :rake, 'log:clear' action :rake, 'log:clear'
end end
@ -217,8 +207,7 @@ class ActionsTest < Rails::Generators::TestCase
end end
def test_env_option_should_win_over_rails_env_variable_when_running_rake def test_env_option_should_win_over_rails_env_variable_when_running_rake
@mock_generator.expect(:call, nil, ['rake log:clear RAILS_ENV=production', verbose: false]) assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
generator.stub(:run, @mock_generator) do
with_rails_env "staging" do with_rails_env "staging" do
action :rake, 'log:clear', env: 'production' action :rake, 'log:clear', env: 'production'
end end
@ -226,8 +215,7 @@ class ActionsTest < Rails::Generators::TestCase
end end
def test_rake_with_sudo_option_should_run_rake_command_with_sudo def test_rake_with_sudo_option_should_run_rake_command_with_sudo
@mock_generator.expect(:call, nil, ["sudo rake log:clear RAILS_ENV=development", verbose: false]) assert_called_with(generator, :run, ["sudo rake log:clear RAILS_ENV=development", verbose: false]) do
generator.stub(:run, @mock_generator) do
with_rails_env nil do with_rails_env nil do
action :rake, 'log:clear', sudo: true action :rake, 'log:clear', sudo: true
end end
@ -235,8 +223,7 @@ class ActionsTest < Rails::Generators::TestCase
end end
def test_capify_should_run_the_capify_command def test_capify_should_run_the_capify_command
@mock_generator.expect(:call, nil, ['capify .', verbose: false]) assert_called_with(generator, :run, ['capify .', verbose: false]) do
generator.stub(:run, @mock_generator) do
action :capify! action :capify!
end end
end end
@ -274,8 +261,7 @@ F
def test_readme def test_readme
run_generator run_generator
2.times { @mock_generator.expect(:call, destination_root,[]) } assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do
Rails::Generators::AppGenerator.stub(:source_root, @mock_generator) do
assert_match "application up and running", action(:readme, "README.md") assert_match "application up and running", action(:readme, "README.md")
end end
end end
@ -283,8 +269,7 @@ F
def test_readme_with_quiet def test_readme_with_quiet
generator(default_arguments, quiet: true) generator(default_arguments, quiet: true)
run_generator run_generator
2.times { @mock_generator.expect(:call, destination_root,[]) } assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do
Rails::Generators::AppGenerator.stub(:source_root, @mock_generator) do
assert_no_match "application up and running", action(:readme, "README.md") assert_no_match "application up and running", action(:readme, "README.md")
end end
end end

View file

@ -1,6 +1,7 @@
require 'abstract_unit' require 'abstract_unit'
require 'active_support/core_ext/module/remove_method' require 'active_support/core_ext/module/remove_method'
require 'active_support/testing/stream' require 'active_support/testing/stream'
require 'active_support/testing/method_call_assertions'
require 'rails/generators' require 'rails/generators'
require 'rails/generators/test_case' require 'rails/generators/test_case'
@ -25,6 +26,7 @@ require 'action_view'
module GeneratorsTestHelper module GeneratorsTestHelper
include ActiveSupport::Testing::Stream include ActiveSupport::Testing::Stream
include ActiveSupport::Testing::MethodCallAssertions
def self.included(base) def self.included(base)
base.class_eval do base.class_eval do

View file

@ -1,7 +1,6 @@
require 'generators/generators_test_helper' require 'generators/generators_test_helper'
require 'rails/generators/rails/model/model_generator' require 'rails/generators/rails/model/model_generator'
require 'rails/generators/test_unit/model/model_generator' require 'rails/generators/test_unit/model/model_generator'
require 'minitest/mock'
class GeneratorsTest < Rails::Generators::TestCase class GeneratorsTest < Rails::Generators::TestCase
include GeneratorsTestHelper include GeneratorsTestHelper
@ -9,18 +8,15 @@ class GeneratorsTest < Rails::Generators::TestCase
def setup def setup
@path = File.expand_path("lib", Rails.root) @path = File.expand_path("lib", Rails.root)
$LOAD_PATH.unshift(@path) $LOAD_PATH.unshift(@path)
@mock_generator = MiniTest::Mock.new
end end
def teardown def teardown
$LOAD_PATH.delete(@path) $LOAD_PATH.delete(@path)
@mock_generator.verify
end end
def test_simple_invoke def test_simple_invoke
assert File.exist?(File.join(@path, 'generators', 'model_generator.rb')) assert File.exist?(File.join(@path, 'generators', 'model_generator.rb'))
@mock_generator.expect(:call, nil, [["Account"],{}]) assert_called_with(TestUnit::Generators::ModelGenerator, :start, [["Account"], {}]) do
TestUnit::Generators::ModelGenerator.stub(:start, @mock_generator) do
Rails::Generators.invoke("test_unit:model", ["Account"]) Rails::Generators.invoke("test_unit:model", ["Account"])
end end
end end
@ -51,23 +47,20 @@ class GeneratorsTest < Rails::Generators::TestCase
def test_should_give_higher_preference_to_rails_generators def test_should_give_higher_preference_to_rails_generators
assert File.exist?(File.join(@path, 'generators', 'model_generator.rb')) assert File.exist?(File.join(@path, 'generators', 'model_generator.rb'))
@mock_generator.expect(:call, nil, [["Account"],{}]) assert_called_with(Rails::Generators::ModelGenerator, :start, [["Account"], {}]) do
Rails::Generators::ModelGenerator.stub(:start, @mock_generator) do
warnings = capture(:stderr){ Rails::Generators.invoke :model, ["Account"] } warnings = capture(:stderr){ Rails::Generators.invoke :model, ["Account"] }
assert warnings.empty? assert warnings.empty?
end end
end end
def test_invoke_with_default_values def test_invoke_with_default_values
@mock_generator.expect(:call, nil, [["Account"],{}]) assert_called_with(Rails::Generators::ModelGenerator, :start, [["Account"], {}]) do
Rails::Generators::ModelGenerator.stub(:start, @mock_generator) do
Rails::Generators.invoke :model, ["Account"] Rails::Generators.invoke :model, ["Account"]
end end
end end
def test_invoke_with_config_values def test_invoke_with_config_values
@mock_generator.expect(:call, nil, [["Account"],{behavior: :skip}]) assert_called_with(Rails::Generators::ModelGenerator, :start, [["Account"], behavior: :skip]) do
Rails::Generators::ModelGenerator.stub(:start, @mock_generator) do
Rails::Generators.invoke :model, ["Account"], behavior: :skip Rails::Generators.invoke :model, ["Account"], behavior: :skip
end end
end end
@ -115,8 +108,7 @@ class GeneratorsTest < Rails::Generators::TestCase
def test_invoke_with_nested_namespaces def test_invoke_with_nested_namespaces
model_generator = Minitest::Mock.new model_generator = Minitest::Mock.new
model_generator.expect(:start, nil, [["Account"], {}]) model_generator.expect(:start, nil, [["Account"], {}])
@mock_generator.expect(:call, model_generator, ['namespace', 'my:awesome']) assert_called_with(Rails::Generators, :find_by_namespace, ['namespace', 'my:awesome'], returns: model_generator) do
Rails::Generators.stub(:find_by_namespace, @mock_generator) do
Rails::Generators.invoke 'my:awesome:namespace', ["Account"] Rails::Generators.invoke 'my:awesome:namespace', ["Account"]
end end
model_generator.verify model_generator.verify
@ -185,8 +177,7 @@ class GeneratorsTest < Rails::Generators::TestCase
def test_fallbacks_for_generators_on_invoke def test_fallbacks_for_generators_on_invoke
Rails::Generators.fallbacks[:shoulda] = :test_unit Rails::Generators.fallbacks[:shoulda] = :test_unit
@mock_generator.expect(:call, nil, [["Account"],{}]) assert_called_with(TestUnit::Generators::ModelGenerator, :start, [["Account"], {}]) do
TestUnit::Generators::ModelGenerator.stub(:start, @mock_generator) do
Rails::Generators.invoke "shoulda:model", ["Account"] Rails::Generators.invoke "shoulda:model", ["Account"]
end end
ensure ensure
@ -196,8 +187,7 @@ class GeneratorsTest < Rails::Generators::TestCase
def test_nested_fallbacks_for_generators def test_nested_fallbacks_for_generators
Rails::Generators.fallbacks[:shoulda] = :test_unit Rails::Generators.fallbacks[:shoulda] = :test_unit
Rails::Generators.fallbacks[:super_shoulda] = :shoulda Rails::Generators.fallbacks[:super_shoulda] = :shoulda
@mock_generator.expect(:call, nil, [["Account"],{}]) assert_called_with(TestUnit::Generators::ModelGenerator, :start, [["Account"], {}]) do
TestUnit::Generators::ModelGenerator.stub(:start, @mock_generator) do
Rails::Generators.invoke "super_shoulda:model", ["Account"] Rails::Generators.invoke "super_shoulda:model", ["Account"]
end end
ensure ensure