mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Reorganise test integration
Adds compatibility with minitest-rails 0.5
This commit is contained in:
parent
05e64678ab
commit
eca41450ce
17 changed files with 171 additions and 180 deletions
8
Rakefile
8
Rakefile
|
@ -16,7 +16,7 @@ desc "Run all specs"
|
|||
task "spec" => "spec:all"
|
||||
|
||||
namespace "spec" do
|
||||
task "all" => ["draper", "generators", "minitest-rails", "integration"]
|
||||
task "all" => ["draper", "generators", "integration"]
|
||||
|
||||
def spec_task(name)
|
||||
desc "Run #{name} specs"
|
||||
|
@ -27,7 +27,6 @@ namespace "spec" do
|
|||
|
||||
spec_task "draper"
|
||||
spec_task "generators"
|
||||
spec_task "minitest-rails"
|
||||
|
||||
desc "Run integration specs"
|
||||
task "integration" => ["db:setup", "integration:all"]
|
||||
|
@ -51,6 +50,8 @@ namespace "spec" do
|
|||
end
|
||||
|
||||
task "test" do
|
||||
puts "Running rake in dummy app"
|
||||
ENV["RAILS_ENV"] = "test"
|
||||
run_in_dummy_app "rake"
|
||||
end
|
||||
end
|
||||
|
@ -60,7 +61,8 @@ namespace "db" do
|
|||
desc "Set up databases for integration testing"
|
||||
task "setup" do
|
||||
run_in_dummy_app "rm -f db/*.sqlite3"
|
||||
run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed db:test:prepare"
|
||||
run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed"
|
||||
run_in_dummy_app "RAILS_ENV=production rake db:schema:load db:seed"
|
||||
run_in_dummy_app "RAILS_ENV=test rake db:schema:load"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,10 +17,11 @@ module Draper
|
|||
config.after_initialize do |app|
|
||||
app.config.paths.add 'app/decorators', eager_load: true
|
||||
|
||||
# Test Support
|
||||
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
|
||||
require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
|
||||
require 'draper/test/test_unit_integration'
|
||||
unless Rails.env.production?
|
||||
require 'draper/test_case'
|
||||
require 'draper/test/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
|
||||
require 'draper/test/minitest_integration' if defined?(MiniTest::Rails)
|
||||
end
|
||||
end
|
||||
|
||||
initializer "draper.setup_action_controller" do |app|
|
||||
|
|
22
lib/draper/tasks/test.rake
Normal file
22
lib/draper/tasks/test.rake
Normal file
|
@ -0,0 +1,22 @@
|
|||
require 'rake/testtask'
|
||||
|
||||
test_task = if Rails.version.start_with?("3.0")
|
||||
require 'rails/test_unit/railtie'
|
||||
Rake::TestTask
|
||||
else
|
||||
require 'rails/test_unit/sub_test_task'
|
||||
Rails::SubTestTask
|
||||
end
|
||||
|
||||
namespace :test do
|
||||
test_task.new(:decorators => "test:prepare") do |t|
|
||||
t.libs << "test"
|
||||
t.pattern = "test/decorators/**/*_test.rb"
|
||||
end
|
||||
end
|
||||
|
||||
if Rake::Task.task_defined?('test:run')
|
||||
Rake::Task['test:run'].enhance do
|
||||
Rake::Task['test:decorators'].invoke
|
||||
end
|
||||
end
|
|
@ -1,5 +0,0 @@
|
|||
if Rake::Task.task_defined?('test:run')
|
||||
Rake::Task['test:run'].enhance do
|
||||
Rake::Task['test:decorators'].invoke
|
||||
end
|
||||
end
|
|
@ -1,31 +1,6 @@
|
|||
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
|
||||
class Draper::TestCase
|
||||
register_spec_type(self) do |desc|
|
||||
desc < Draper::Decorator || desc < Draper::CollectionDecorator if desc.is_a?(Class)
|
||||
end
|
||||
|
||||
register_spec_type(/Decorator( ?Test)?\z/i, self)
|
||||
end
|
||||
|
|
|
@ -1,37 +1,12 @@
|
|||
module Draper
|
||||
module RSpec
|
||||
module DecoratorExampleGroup
|
||||
include Draper::TestCase::Behavior
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module DecoratorExampleGroup
|
||||
extend ActiveSupport::Concern
|
||||
included { metadata[:type] = :decorator }
|
||||
|
||||
include Draper::ViewHelpers::ClassMethods
|
||||
alias_method :helper, :helpers
|
||||
end
|
||||
|
||||
::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
|
||||
|
||||
class Railtie < Rails::Railtie
|
||||
config.after_initialize do |app|
|
||||
::RSpec.configure do |rspec|
|
||||
if defined?(Capybara)
|
||||
require 'capybara/rspec/matchers'
|
||||
rspec.include Capybara::RSpecMatchers, :type => :decorator
|
||||
end
|
||||
|
||||
if defined?(Devise)
|
||||
require 'draper/test/devise_helper'
|
||||
rspec.include Draper::DeviseHelper, :type => :decorator
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
included { metadata[:type] = :decorator }
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include DecoratorExampleGroup, example_group: {file_path: %r{spec/decorators}}, type: :decorator
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
require "rake/testtask"
|
||||
|
||||
klass = nil
|
||||
|
||||
if Rails.version[0,3] == "3.0"
|
||||
require 'rails/test_unit/railtie'
|
||||
klass = Rake::TestTask
|
||||
else
|
||||
require "rails/test_unit/sub_test_task"
|
||||
klass = Rails::SubTestTask
|
||||
end
|
||||
|
||||
namespace :test do
|
||||
klass.new(:decorators => "test:prepare") do |t|
|
||||
t.libs << "test"
|
||||
t.pattern = "test/decorators/**/*_test.rb"
|
||||
end
|
||||
end
|
33
lib/draper/test_case.rb
Normal file
33
lib/draper/test_case.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
module Draper
|
||||
begin
|
||||
require 'minitest/rails'
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
active_support_test_case = begin
|
||||
require 'minitest/rails/active_support' # minitest-rails < 0.5
|
||||
::MiniTest::Rails::ActiveSupport::TestCase
|
||||
rescue LoadError
|
||||
require 'active_support/test_case'
|
||||
::ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
class TestCase < active_support_test_case
|
||||
module Behavior
|
||||
if defined?(::Devise)
|
||||
require 'draper/test/devise_helper'
|
||||
include Draper::DeviseHelper
|
||||
end
|
||||
|
||||
if defined?(::Capybara) && (defined?(::RSpec) || defined?(::MiniTest::Matchers))
|
||||
require 'capybara/rspec/matchers'
|
||||
include ::Capybara::RSpecMatchers
|
||||
end
|
||||
|
||||
include Draper::ViewHelpers::ClassMethods
|
||||
alias_method :helper, :helpers
|
||||
end
|
||||
|
||||
include Behavior
|
||||
end
|
||||
end
|
|
@ -1,4 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class <%= class_name %>DecoratorTest < ActiveSupport::TestCase
|
||||
class <%= class_name %>DecoratorTest < Draper::TestCase
|
||||
end
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
require 'rspec/core/rake_task'
|
||||
|
||||
RSpec::Core::RakeTask.new
|
||||
|
||||
task "default" => "spec"
|
10
spec/dummy/lib/tasks/test.rake
Normal file
10
spec/dummy/lib/tasks/test.rake
Normal file
|
@ -0,0 +1,10 @@
|
|||
require 'rspec/core/rake_task'
|
||||
require 'rake/testtask'
|
||||
|
||||
RSpec::Core::RakeTask.new :rspec
|
||||
|
||||
Rake::TestTask.new :mini_test do |t|
|
||||
t.test_files = ["mini_test/mini_test_integration_test.rb"]
|
||||
end
|
||||
|
||||
task :default => [:rspec, :mini_test]
|
46
spec/dummy/mini_test/mini_test_integration_test.rb
Normal file
46
spec/dummy/mini_test/mini_test_integration_test.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
require File.expand_path('../../config/environment', __FILE__)
|
||||
require 'minitest/autorun'
|
||||
require 'minitest/rails'
|
||||
|
||||
def it_is_a_decorator_test
|
||||
it "is a decorator test" do
|
||||
assert_kind_of Draper::TestCase, self
|
||||
end
|
||||
end
|
||||
|
||||
def it_is_not_a_decorator_test
|
||||
it "is not a decorator test" do
|
||||
refute_kind_of Draper::TestCase, self
|
||||
end
|
||||
end
|
||||
|
||||
ProductDecorator = Class.new(Draper::Decorator)
|
||||
ProductsDecorator = Class.new(Draper::CollectionDecorator)
|
||||
|
||||
describe ProductDecorator do
|
||||
it_is_a_decorator_test
|
||||
end
|
||||
|
||||
describe ProductsDecorator do
|
||||
it_is_a_decorator_test
|
||||
end
|
||||
|
||||
describe "ProductDecorator" do
|
||||
it_is_a_decorator_test
|
||||
end
|
||||
|
||||
describe "AnyDecorator" do
|
||||
it_is_a_decorator_test
|
||||
end
|
||||
|
||||
describe "Any decorator test" do
|
||||
it_is_a_decorator_test
|
||||
end
|
||||
|
||||
describe Object do
|
||||
it_is_not_a_decorator_test
|
||||
end
|
||||
|
||||
describe "Nope" do
|
||||
it_is_not_a_decorator_test
|
||||
end
|
19
spec/dummy/spec/decorators/rspec_integration_spec.rb
Normal file
19
spec/dummy/spec/decorators/rspec_integration_spec.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
describe "A spec in this folder" do
|
||||
it "is a decorator spec" do
|
||||
example.metadata[:type].should be :decorator
|
||||
end
|
||||
end
|
||||
|
||||
describe "A decorator spec" do
|
||||
it "can access helpers through `helper`" do
|
||||
helper.content_tag(:p, "Help!").should == "<p>Help!</p>"
|
||||
end
|
||||
|
||||
it "can access helpers through `helpers`" do
|
||||
helpers.content_tag(:p, "Help!").should == "<p>Help!</p>"
|
||||
end
|
||||
|
||||
it "can access helpers through `h`" do
|
||||
h.content_tag(:p, "Help!").should == "<p>Help!</p>"
|
||||
end
|
||||
end
|
|
@ -1,7 +1,6 @@
|
|||
ENV['RAILS_ENV'] ||= 'test'
|
||||
require File.expand_path('../../config/environment', __FILE__)
|
||||
require 'rspec/rails'
|
||||
require 'draper/test/rspec_integration'
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.treat_symbols_as_metadata_keys_with_true_values = true
|
||||
|
|
|
@ -80,7 +80,7 @@ describe Rails::Generators::DecoratorGenerator do
|
|||
describe 'test/decorators/YourModel_decorator_test.rb' do
|
||||
subject { file('test/decorators/your_model_decorator_test.rb') }
|
||||
it { should exist }
|
||||
it { should contain "class YourModelDecoratorTest < ActiveSupport::TestCase" }
|
||||
it { should contain "class YourModelDecoratorTest < Draper::TestCase" }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -90,7 +90,7 @@ describe Rails::Generators::DecoratorGenerator do
|
|||
describe 'test/decorators/your_model_decorator_test.rb' do
|
||||
subject { file('test/decorators/namespace/your_model_decorator_test.rb') }
|
||||
it { should exist }
|
||||
it { should contain "class Namespace::YourModelDecoratorTest < ActiveSupport::TestCase" }
|
||||
it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
require 'spec_helper'
|
||||
require 'minitest/rails/active_support'
|
||||
require 'draper/test/minitest_integration'
|
||||
|
||||
describe "minitest-rails spec_type Lookup Integration" do
|
||||
context "ProductDecorator" do
|
||||
it "resolves constants" do
|
||||
klass = MiniTest::Spec.spec_type(ProductDecorator)
|
||||
klass.should be Draper::MiniTest::DecoratorTestCase
|
||||
end
|
||||
|
||||
it "resolves strings" do
|
||||
klass = MiniTest::Spec.spec_type("ProductDecorator")
|
||||
klass.should be Draper::MiniTest::DecoratorTestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "WidgetDecorator" do
|
||||
it "resolves constants" do
|
||||
klass = MiniTest::Spec.spec_type(WidgetDecorator)
|
||||
klass.should be Draper::MiniTest::DecoratorTestCase
|
||||
end
|
||||
|
||||
it "resolves strings" do
|
||||
klass = MiniTest::Spec.spec_type("WidgetDecorator")
|
||||
klass.should be Draper::MiniTest::DecoratorTestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "decorator strings" do
|
||||
it "resolves DoesNotExistDecorator" do
|
||||
klass = MiniTest::Spec.spec_type("DoesNotExistDecorator")
|
||||
klass.should be Draper::MiniTest::DecoratorTestCase
|
||||
end
|
||||
|
||||
it "resolves DoesNotExistDecoratorTest" do
|
||||
klass = MiniTest::Spec.spec_type("DoesNotExistDecoratorTest")
|
||||
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 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 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 be MiniTest::Spec
|
||||
end
|
||||
|
||||
it "doesn't resolve strings" do
|
||||
klass = MiniTest::Spec.spec_type("Nothing to see here...")
|
||||
klass.should be MiniTest::Spec
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,25 +8,25 @@ require 'action_controller/test_case'
|
|||
|
||||
Bundler.require
|
||||
|
||||
require './spec/support/active_model'
|
||||
require './spec/support/active_record'
|
||||
require './spec/support/action_controller'
|
||||
require 'support/active_model'
|
||||
require 'support/active_record'
|
||||
require 'support/action_controller'
|
||||
|
||||
require './spec/support/models/product'
|
||||
require './spec/support/models/namespaced_product'
|
||||
require './spec/support/models/non_active_model_product'
|
||||
require './spec/support/models/widget'
|
||||
require './spec/support/models/some_thing'
|
||||
require './spec/support/models/uninferrable_decorator_model'
|
||||
require 'support/models/product'
|
||||
require 'support/models/namespaced_product'
|
||||
require 'support/models/non_active_model_product'
|
||||
require 'support/models/widget'
|
||||
require 'support/models/some_thing'
|
||||
require 'support/models/uninferrable_decorator_model'
|
||||
|
||||
require './spec/support/decorators/product_decorator'
|
||||
require './spec/support/decorators/namespaced_product_decorator'
|
||||
require './spec/support/decorators/non_active_model_product_decorator'
|
||||
require './spec/support/decorators/widget_decorator'
|
||||
require './spec/support/decorators/specific_product_decorator'
|
||||
require './spec/support/decorators/products_decorator'
|
||||
require './spec/support/decorators/some_thing_decorator'
|
||||
require './spec/support/decorators/decorator_with_application_helper'
|
||||
require 'support/decorators/product_decorator'
|
||||
require 'support/decorators/namespaced_product_decorator'
|
||||
require 'support/decorators/non_active_model_product_decorator'
|
||||
require 'support/decorators/widget_decorator'
|
||||
require 'support/decorators/specific_product_decorator'
|
||||
require 'support/decorators/products_decorator'
|
||||
require 'support/decorators/some_thing_decorator'
|
||||
require 'support/decorators/decorator_with_application_helper'
|
||||
|
||||
class << Rails
|
||||
undef application # Avoid silly Rails bug: https://github.com/rails/rails/pull/6429
|
||||
|
|
Loading…
Add table
Reference in a new issue