Add a RSpec generator and hook to create default decorator specs

This commit is contained in:
Benny Wong 2011-10-16 11:16:28 -04:00
parent 72f87569a3
commit fa4f2ebbd5
5 changed files with 58 additions and 1 deletions

View File

@ -13,5 +13,7 @@ module Draper
end
template 'decorator.rb', "#{DECORATORS_ROOT}#{singular_name}_decorator.rb"
end
hook_for :test_framework
end
end
end

View File

@ -0,0 +1,17 @@
module Rspec
class DecoratorGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
SPEC_ROOT = 'spec/decorators/'
APPLICATION_DECORATOR_SPEC = 'application_decorator_spec.rb'
APPLICATION_DECORATOR_SPEC_PATH = SPEC_ROOT + APPLICATION_DECORATOR_SPEC
def build_model_and_application_decorator_specs
empty_directory SPEC_ROOT
unless File.exists?(APPLICATION_DECORATOR_SPEC_PATH)
template APPLICATION_DECORATOR_SPEC, APPLICATION_DECORATOR_SPEC_PATH
end
template 'decorator_spec.rb', "#{SPEC_ROOT}#{singular_name}_decorator_spec.rb"
end
end
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe ApplicationDecorator do
before { ApplicationController.new.set_current_view_context }
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe <%= singular_name.camelize %>Decorator do
before { ApplicationController.new.set_current_view_context }
end

View File

@ -0,0 +1,28 @@
require 'spec_helper'
# Generators are not automatically loaded by Rails
require 'generators/rspec/decorator_generator'
describe Rspec::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 'spec/decorators/application_decorator_spec.rb' do
subject { file('spec/decorators/application_decorator_spec.rb') }
it { should exist }
it { should contain "describe ApplicationDecorator do" }
end
describe 'spec/decorators/products_decorator_spec.rb' do
subject { file('spec/decorators/products_decorator_spec.rb') }
it { should exist }
it { should contain "describe ProductsDecorator" }
end
end
end