From 551961e72ee92355bc9c848bedfcc573856d12b0 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Wed, 9 Jan 2013 22:27:30 +0000 Subject: [PATCH] Add `helper` method to tests --- lib/draper/test/devise_helper.rb | 34 ++++++++++++ lib/draper/test/minitest_integration.rb | 34 ++++++++++-- lib/draper/test/rspec_integration.rb | 70 +++++++------------------ spec/minitest-rails/spec_type_spec.rb | 20 +++---- 4 files changed, 93 insertions(+), 65 deletions(-) create mode 100644 lib/draper/test/devise_helper.rb diff --git a/lib/draper/test/devise_helper.rb b/lib/draper/test/devise_helper.rb new file mode 100644 index 00000000..3a55b1ac --- /dev/null +++ b/lib/draper/test/devise_helper.rb @@ -0,0 +1,34 @@ +module Draper + module DeviseHelper + def sign_in(user) + warden.stub :authenticate! => user + controller.stub :current_user => user + user + end + + private + + def request + @request ||= ::ActionDispatch::TestRequest.new + end + + def controller + return @controller if @controller + @controller = ApplicationController.new + @controller.request = request + ::Draper::ViewContext.current = @controller.view_context + @controller + end + + # taken from Devise's helper but uses the request method instead of @request + # and we don't really need the rest of their helper + def warden + @warden ||= begin + manager = Warden::Manager.new(nil) do |config| + config.merge! Devise.warden_config + end + request.env['warden'] = Warden::Proxy.new(request.env, manager) + end + end + end +end diff --git a/lib/draper/test/minitest_integration.rb b/lib/draper/test/minitest_integration.rb index 2fe64b98..b772383a 100755 --- a/lib/draper/test/minitest_integration.rb +++ b/lib/draper/test/minitest_integration.rb @@ -1,7 +1,31 @@ -class MiniTest::Rails::ActiveSupport::TestCase - # Use AS::TestCase for the base class when describing a decorator - register_spec_type(self) do |desc| - desc < Draper::Decorator if desc.is_a?(Class) +require 'minitest/rails/active_support' + +module Draper + module MiniTest + + class DecoratorTestCase < ::MiniTest::Rails::ActiveSupport::TestCase + include Draper::ViewHelpers::ClassMethods + alias_method :helper, :helpers + + register_spec_type(self) do |desc| + desc < Draper::Decorator if desc.is_a?(Class) + end + register_spec_type(/Decorator( ?Test)?\z/i, self) + end + + class Railtie < Rails::Railtie + config.after_initialize do |app| + if defined?(Capybara) + require 'capybara/rspec/matchers' + DecoratorTestCase.send :include, Capybara::RSpecMatchers + end + + if defined?(Devise) + require 'draper/test/devise_helper' + DecoratorTestCase.send :include, Draper::DeviseHelper + end + end + end end - register_spec_type(/Decorator( ?Test)?\z/i, self) + end diff --git a/lib/draper/test/rspec_integration.rb b/lib/draper/test/rspec_integration.rb index 277cd0b7..688e4808 100755 --- a/lib/draper/test/rspec_integration.rb +++ b/lib/draper/test/rspec_integration.rb @@ -1,67 +1,37 @@ module Draper - module DecoratorExampleGroup - extend ActiveSupport::Concern - included { metadata[:type] = :decorator } - end - - module DeviseHelper - def sign_in(user) - warden.stub :authenticate! => user - controller.stub :current_user => user - user - end + module RSpec - private + module DecoratorExampleGroup + extend ActiveSupport::Concern + included { metadata[:type] = :decorator } - def request - @request ||= ::ActionDispatch::TestRequest.new + include Draper::ViewHelpers::ClassMethods + alias_method :helper, :helpers end - def controller - return @controller if @controller - @controller = ApplicationController.new - @controller.request = request - ::Draper::ViewContext.current = @controller.view_context - @controller + ::RSpec.configure do |config| + # Automatically tag specs in specs/decorators as type: :decorator + config.include DecoratorExampleGroup, :type => :decorator, :example_group => { + :file_path => %r{spec/decorators} + } end - # taken from Devise's helper but uses the request method instead of @request - # and we don't really need the rest of their helper - def warden - @warden ||= begin - manager = Warden::Manager.new(nil) do |config| - config.merge! Devise.warden_config - end - request.env['warden'] = Warden::Proxy.new(request.env, manager) - end - end - end -end - -RSpec.configure do |config| - # Automatically tag specs in specs/decorators as type: :decorator - config.include Draper::DecoratorExampleGroup, :type => :decorator, :example_group => { - :file_path => /spec[\\\/]decorators/ - } - - if defined?(Devise) - config.include Draper::DeviseHelper, :type => :decorator - end -end - -module Draper - module RSpec class Railtie < Rails::Railtie config.after_initialize do |app| - if defined?(Capybara) - require 'capybara/rspec/matchers' + ::RSpec.configure do |rspec| + if defined?(Capybara) + require 'capybara/rspec/matchers' + rspec.include Capybara::RSpecMatchers, :type => :decorator + end - ::RSpec.configure do |config| - config.include Capybara::RSpecMatchers, :type => :decorator + if defined?(Devise) + require 'draper/test/devise_helper' + rspec.include Draper::DeviseHelper, :type => :decorator end end end end + end end diff --git a/spec/minitest-rails/spec_type_spec.rb b/spec/minitest-rails/spec_type_spec.rb index 5a20407e..4773991d 100644 --- a/spec/minitest-rails/spec_type_spec.rb +++ b/spec/minitest-rails/spec_type_spec.rb @@ -6,58 +6,58 @@ context "ProductDecorator" do it "resolves constants" do klass = MiniTest::Spec.spec_type(ProductDecorator) - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end it "resolves strings" do klass = MiniTest::Spec.spec_type("ProductDecorator") - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end end context "WidgetDecorator" do it "resolves constants" do klass = MiniTest::Spec.spec_type(WidgetDecorator) - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end it "resolves strings" do klass = MiniTest::Spec.spec_type("WidgetDecorator") - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end end context "decorator strings" do it "resolves DoesNotExistDecorator" do klass = MiniTest::Spec.spec_type("DoesNotExistDecorator") - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end it "resolves DoesNotExistDecoratorTest" do klass = MiniTest::Spec.spec_type("DoesNotExistDecoratorTest") - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end it "resolves Does Not Exist Decorator" do klass = MiniTest::Spec.spec_type("Does Not Exist Decorator") - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end it "resolves Does Not Exist Decorator Test" do klass = MiniTest::Spec.spec_type("Does Not Exist Decorator Test") - klass.should == MiniTest::Rails::ActiveSupport::TestCase + klass.should be Draper::MiniTest::DecoratorTestCase end end context "non-decorators" do it "doesn't resolve constants" do klass = MiniTest::Spec.spec_type(Draper::HelperSupport) - klass.should == MiniTest::Spec + klass.should be MiniTest::Spec end it "doesn't resolve strings" do klass = MiniTest::Spec.spec_type("Nothing to see here...") - klass.should == MiniTest::Spec + klass.should be MiniTest::Spec end end end