From ca3cf912e079336561159e7ee68d5b6f858c7c1a Mon Sep 17 00:00:00 2001 From: Benny Wong Date: Wed, 19 Oct 2011 22:54:35 -0400 Subject: [PATCH] Add a Test::Unit generator and hook to create default decorator tests --- .../test_unit/decorator_generator.rb | 17 +++++++++++ .../templates/application_decorator_test.rb | 11 ++++++++ .../test_unit/templates/decorator_test.rb | 12 ++++++++ .../test_unit/decorator_generator_spec.rb | 28 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 lib/generators/test_unit/decorator_generator.rb create mode 100644 lib/generators/test_unit/templates/application_decorator_test.rb create mode 100644 lib/generators/test_unit/templates/decorator_test.rb create mode 100644 spec/generators/test_unit/decorator_generator_spec.rb diff --git a/lib/generators/test_unit/decorator_generator.rb b/lib/generators/test_unit/decorator_generator.rb new file mode 100644 index 0000000..7a553c0 --- /dev/null +++ b/lib/generators/test_unit/decorator_generator.rb @@ -0,0 +1,17 @@ +module TestUnit + class DecoratorGenerator < ::Rails::Generators::NamedBase + source_root File.expand_path('../templates', __FILE__) + + TEST_ROOT = 'test/decorators/' + APPLICATION_DECORATOR_TEST = 'application_decorator_test.rb' + APPLICATION_DECORATOR_TEST_PATH = TEST_ROOT + APPLICATION_DECORATOR_TEST + + def build_model_and_application_decorator_tests + empty_directory TEST_ROOT + unless File.exists?(APPLICATION_DECORATOR_TEST_PATH) + template APPLICATION_DECORATOR_TEST, APPLICATION_DECORATOR_TEST_PATH + end + template 'decorator_test.rb', "#{TEST_ROOT}#{singular_name}_decorator_test.rb" + end + end +end diff --git a/lib/generators/test_unit/templates/application_decorator_test.rb b/lib/generators/test_unit/templates/application_decorator_test.rb new file mode 100644 index 0000000..d77ec0a --- /dev/null +++ b/lib/generators/test_unit/templates/application_decorator_test.rb @@ -0,0 +1,11 @@ +require 'test_helper' + +class ApplicationDecoratorTest < ActiveSupport::TestCase + def setup + ApplicationController.new.set_current_view_context + end + + # test "the truth" do + # assert true + # end +end diff --git a/lib/generators/test_unit/templates/decorator_test.rb b/lib/generators/test_unit/templates/decorator_test.rb new file mode 100644 index 0000000..dce4bf1 --- /dev/null +++ b/lib/generators/test_unit/templates/decorator_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class <%= singular_name.camelize %>DecoratorTest < ActiveSupport::TestCase + def setup + ApplicationController.new.set_current_view_context + end + + # test "the truth" do + # assert true + # end +end + diff --git a/spec/generators/test_unit/decorator_generator_spec.rb b/spec/generators/test_unit/decorator_generator_spec.rb new file mode 100644 index 0000000..7d71f04 --- /dev/null +++ b/spec/generators/test_unit/decorator_generator_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +# Generators are not automatically loaded by Rails +require 'generators/test_unit/decorator_generator' + +describe TestUnit::DecoratorGenerator do + # Tell the generator where to put its output (what it thinks of as Rails.root) + destination File.expand_path("../../../../tmp", __FILE__) + + before { prepare_destination } + + describe 'no arguments' do + before { run_generator %w(products) } + + describe 'test/decorators/application_decorator_test.rb' do + subject { file('test/decorators/application_decorator_test.rb') } + it { should exist } + it { should contain "class ApplicationDecoratorTest < ActiveSupport::TestCase" } + end + + describe 'test/decorators/products_decorator_test.rb' do + subject { file('test/decorators/products_decorator_test.rb') } + it { should exist } + it { should contain "class ProductsDecoratorTest < ActiveSupport::TestCase" } + end + + end +end