2013-01-25 13:44:36 -05:00
|
|
|
require 'isolation/abstract_unit'
|
2013-02-06 01:03:17 -05:00
|
|
|
require 'active_support/core_ext/string/strip'
|
2013-01-25 13:44:36 -05:00
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
class TestRunnerTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
2013-03-11 14:28:43 -04:00
|
|
|
ENV['RAILS_ENV'] = nil
|
2013-01-25 13:44:36 -05:00
|
|
|
create_schema
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2013-03-11 14:28:43 -04:00
|
|
|
def test_run_in_test_environment
|
|
|
|
app_file 'test/unit/env_test.rb', <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class EnvTest < ActiveSupport::TestCase
|
|
|
|
def test_env
|
|
|
|
puts "Current Environment: \#{Rails.env}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "Current Environment: test", run_test_command('test/unit/env_test.rb')
|
2013-03-11 14:28:43 -04:00
|
|
|
end
|
|
|
|
|
2013-01-25 13:44:36 -05:00
|
|
|
def test_run_single_file
|
|
|
|
create_test_file :models, 'foo'
|
2013-03-29 22:22:02 -04:00
|
|
|
create_test_file :models, 'bar'
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "1 runs, 1 assertions, 0 failures", run_test_command("test/models/foo_test.rb")
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_multiple_files
|
|
|
|
create_test_file :models, 'foo'
|
|
|
|
create_test_file :models, 'bar'
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "2 runs, 2 assertions, 0 failures", run_test_command("test/models/foo_test.rb test/models/bar_test.rb")
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_file_with_syntax_error
|
|
|
|
app_file 'test/models/error_test.rb', <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
def; end
|
|
|
|
RUBY
|
|
|
|
|
2015-01-30 06:04:57 -05:00
|
|
|
error = capture(:stderr) { run_test_command('test/models/error_test.rb') }
|
|
|
|
assert_match "syntax error", error
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_models
|
|
|
|
create_test_file :models, 'foo'
|
|
|
|
create_test_file :models, 'bar'
|
|
|
|
create_test_file :controllers, 'foobar_controller'
|
2013-03-29 22:22:02 -04:00
|
|
|
run_test_models_command.tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "FooTest", output
|
|
|
|
assert_match "BarTest", output
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "2 runs, 2 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_helpers
|
|
|
|
create_test_file :helpers, 'foo_helper'
|
|
|
|
create_test_file :helpers, 'bar_helper'
|
|
|
|
create_test_file :controllers, 'foobar_controller'
|
2013-03-29 22:22:02 -04:00
|
|
|
run_test_helpers_command.tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "FooHelperTest", output
|
|
|
|
assert_match "BarHelperTest", output
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "2 runs, 2 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_units
|
|
|
|
create_test_file :models, 'foo'
|
|
|
|
create_test_file :helpers, 'bar_helper'
|
|
|
|
create_test_file :unit, 'baz_unit'
|
|
|
|
create_test_file :controllers, 'foobar_controller'
|
2013-03-29 22:22:02 -04:00
|
|
|
run_test_units_command.tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "FooTest", output
|
|
|
|
assert_match "BarHelperTest", output
|
|
|
|
assert_match "BazUnitTest", output
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "3 runs, 3 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_controllers
|
|
|
|
create_test_file :controllers, 'foo_controller'
|
|
|
|
create_test_file :controllers, 'bar_controller'
|
|
|
|
create_test_file :models, 'foo'
|
2013-03-29 22:22:02 -04:00
|
|
|
run_test_controllers_command.tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "FooControllerTest", output
|
|
|
|
assert_match "BarControllerTest", output
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "2 runs, 2 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_mailers
|
|
|
|
create_test_file :mailers, 'foo_mailer'
|
|
|
|
create_test_file :mailers, 'bar_mailer'
|
|
|
|
create_test_file :models, 'foo'
|
2013-03-29 22:22:02 -04:00
|
|
|
run_test_mailers_command.tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "FooMailerTest", output
|
|
|
|
assert_match "BarMailerTest", output
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "2 runs, 2 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-14 19:23:14 -04:00
|
|
|
def test_run_jobs
|
|
|
|
create_test_file :jobs, 'foo_job'
|
|
|
|
create_test_file :jobs, 'bar_job'
|
|
|
|
create_test_file :models, 'foo'
|
|
|
|
run_test_jobs_command.tap do |output|
|
|
|
|
assert_match "FooJobTest", output
|
|
|
|
assert_match "BarJobTest", output
|
|
|
|
assert_match "2 runs, 2 assertions, 0 failures", output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 13:44:36 -05:00
|
|
|
def test_run_functionals
|
|
|
|
create_test_file :mailers, 'foo_mailer'
|
|
|
|
create_test_file :controllers, 'bar_controller'
|
|
|
|
create_test_file :functional, 'baz_functional'
|
|
|
|
create_test_file :models, 'foo'
|
2013-03-29 22:22:02 -04:00
|
|
|
run_test_functionals_command.tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "FooMailerTest", output
|
|
|
|
assert_match "BarControllerTest", output
|
|
|
|
assert_match "BazFunctionalTest", output
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "3 runs, 3 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_integration
|
|
|
|
create_test_file :integration, 'foo_integration'
|
|
|
|
create_test_file :models, 'foo'
|
2013-03-29 22:22:02 -04:00
|
|
|
run_test_integration_command.tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "FooIntegration", output
|
2013-05-06 20:38:45 -04:00
|
|
|
assert_match "1 runs, 1 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 18:53:11 -05:00
|
|
|
def test_run_all_suites
|
2014-09-14 19:23:14 -04:00
|
|
|
suites = [:models, :helpers, :unit, :controllers, :mailers, :functional, :integration, :jobs]
|
2013-02-07 18:53:11 -05:00
|
|
|
suites.each { |suite| create_test_file suite, "foo_#{suite}" }
|
2013-01-25 13:44:36 -05:00
|
|
|
run_test_command('') .tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
suites.each { |suite| assert_match "Foo#{suite.to_s.camelize}Test", output }
|
2014-09-14 19:23:14 -04:00
|
|
|
assert_match "8 runs, 8 assertions, 0 failures", output
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-01 17:21:09 -05:00
|
|
|
def test_run_named_test
|
|
|
|
app_file 'test/unit/chu_2_koi_test.rb', <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class Chu2KoiTest < ActiveSupport::TestCase
|
|
|
|
def test_rikka
|
|
|
|
puts 'Rikka'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sanae
|
|
|
|
puts 'Sanae'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2013-04-05 19:38:37 -04:00
|
|
|
run_test_command('test/unit/chu_2_koi_test.rb test_rikka').tap do |output|
|
|
|
|
assert_match "Rikka", output
|
|
|
|
assert_no_match "Sanae", output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_matched_test
|
|
|
|
app_file 'test/unit/chu_2_koi_test.rb', <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class Chu2KoiTest < ActiveSupport::TestCase
|
|
|
|
def test_rikka
|
|
|
|
puts 'Rikka'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sanae
|
|
|
|
puts 'Sanae'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
run_test_command('test/unit/chu_2_koi_test.rb /rikka/').tap do |output|
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "Rikka", output
|
|
|
|
assert_no_match "Sanae", output
|
2013-02-01 17:21:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 01:03:17 -05:00
|
|
|
def test_load_fixtures_when_running_test_suites
|
|
|
|
create_model_with_fixture
|
2013-02-07 18:53:11 -05:00
|
|
|
suites = [:models, :helpers, [:units, :unit], :controllers, :mailers,
|
2013-02-06 01:03:17 -05:00
|
|
|
[:functionals, :functional], :integration]
|
|
|
|
|
2013-02-07 18:53:11 -05:00
|
|
|
suites.each do |suite, directory|
|
|
|
|
directory ||= suite
|
2013-02-06 01:03:17 -05:00
|
|
|
create_fixture_test directory
|
2013-03-29 22:22:02 -04:00
|
|
|
assert_match "3 users", run_task(["test:#{suite}"])
|
2013-02-06 01:03:17 -05:00
|
|
|
Dir.chdir(app_path) { FileUtils.rm_f "test/#{directory}" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-05 20:12:23 -04:00
|
|
|
def test_run_with_model
|
|
|
|
create_model_with_fixture
|
|
|
|
create_fixture_test 'models', 'user'
|
|
|
|
assert_match "3 users", run_task(["test models/user"])
|
|
|
|
assert_match "3 users", run_task(["test app/models/user.rb"])
|
|
|
|
end
|
|
|
|
|
2013-03-09 16:03:09 -05:00
|
|
|
def test_run_different_environment_using_env_var
|
|
|
|
app_file 'test/unit/env_test.rb', <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class EnvTest < ActiveSupport::TestCase
|
|
|
|
def test_env
|
|
|
|
puts Rails.env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2013-03-11 14:28:43 -04:00
|
|
|
ENV['RAILS_ENV'] = 'development'
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "development", run_test_command('test/unit/env_test.rb')
|
2013-03-09 16:03:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_different_environment_using_e_tag
|
2013-03-29 22:22:02 -04:00
|
|
|
env = "development"
|
2013-03-09 16:03:09 -05:00
|
|
|
app_file 'test/unit/env_test.rb', <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class EnvTest < ActiveSupport::TestCase
|
|
|
|
def test_env
|
|
|
|
puts Rails.env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2013-03-29 22:22:02 -04:00
|
|
|
assert_match env, run_test_command("test/unit/env_test.rb RAILS_ENV=#{env}")
|
2013-03-09 16:03:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_generated_scaffold_works_with_rails_test
|
|
|
|
create_scaffold
|
2013-03-11 14:48:23 -04:00
|
|
|
assert_match "0 failures, 0 errors, 0 skips", run_test_command('')
|
2013-03-09 16:03:09 -05:00
|
|
|
end
|
|
|
|
|
2013-01-25 13:44:36 -05:00
|
|
|
private
|
2013-03-29 22:22:02 -04:00
|
|
|
def run_task(tasks)
|
|
|
|
Dir.chdir(app_path) { `bundle exec rake #{tasks.join ' '}` }
|
|
|
|
end
|
|
|
|
|
2013-01-25 13:44:36 -05:00
|
|
|
def run_test_command(arguments = 'test/unit/test_test.rb')
|
2013-03-29 22:22:02 -04:00
|
|
|
run_task ['test', arguments]
|
|
|
|
end
|
2014-09-14 19:23:14 -04:00
|
|
|
%w{ mailers models helpers units controllers functionals integration jobs }.each do |type|
|
2013-03-29 22:22:02 -04:00
|
|
|
define_method("run_test_#{type}_command") do
|
|
|
|
run_task ["test:#{type}"]
|
|
|
|
end
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
|
2013-02-06 01:03:17 -05:00
|
|
|
def create_model_with_fixture
|
|
|
|
script 'generate model user name:string'
|
|
|
|
|
|
|
|
app_file 'test/fixtures/users.yml', <<-YAML.strip_heredoc
|
|
|
|
vampire:
|
|
|
|
id: 1
|
|
|
|
name: Koyomi Araragi
|
|
|
|
crab:
|
|
|
|
id: 2
|
|
|
|
name: Senjougahara Hitagi
|
|
|
|
cat:
|
|
|
|
id: 3
|
|
|
|
name: Tsubasa Hanekawa
|
|
|
|
YAML
|
|
|
|
|
2013-03-09 16:03:09 -05:00
|
|
|
run_migration
|
2013-02-06 01:03:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_fixture_test(path = :unit, name = 'test')
|
|
|
|
app_file "test/#{path}/#{name}_test.rb", <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class #{name.camelize}Test < ActiveSupport::TestCase
|
|
|
|
def test_fixture
|
|
|
|
puts "\#{User.count} users (\#{__FILE__})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2013-01-25 13:44:36 -05:00
|
|
|
def create_schema
|
|
|
|
app_file 'db/schema.rb', ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_test_file(path = :unit, name = 'test')
|
|
|
|
app_file "test/#{path}/#{name}_test.rb", <<-RUBY
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class #{name.camelize}Test < ActiveSupport::TestCase
|
|
|
|
def test_truth
|
|
|
|
puts "#{name.camelize}Test"
|
|
|
|
assert true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2013-03-09 16:03:09 -05:00
|
|
|
|
|
|
|
def create_scaffold
|
|
|
|
script 'generate scaffold user name:string'
|
|
|
|
Dir.chdir(app_path) { File.exist?('app/models/user.rb') }
|
|
|
|
run_migration
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_migration
|
|
|
|
Dir.chdir(app_path) { `bundle exec rake db:migrate` }
|
|
|
|
end
|
2013-01-25 13:44:36 -05:00
|
|
|
end
|
|
|
|
end
|