mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Totally change test integration.
We no longer need to do specific things before tests run. The view_context now loads up appropriate things if they don't exist. I ended up getting rid of two spec files; one tested things that were very simple, but since we're not mocking Rails anymore, they're much harder to test. The second was the stuff that I removed. :)
This commit is contained in:
parent
dd3bb0d95d
commit
1dcbe68ee8
7 changed files with 20 additions and 90 deletions
|
@ -1,26 +1,5 @@
|
|||
require 'draper/test/view_context'
|
||||
|
||||
module MiniTest
|
||||
class Spec
|
||||
class Decorator < Spec
|
||||
before { Draper::ViewContext.infect!(self) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class MiniTest::Unit::DecoratorTestCase < MiniTest::Unit::TestCase
|
||||
if method_defined?(:before_setup)
|
||||
# for minitext >= 2.11
|
||||
def before_setup
|
||||
super
|
||||
Draper::ViewContext.infect!(self)
|
||||
end
|
||||
else
|
||||
# for older minitest, like what ships w/Ruby 1.9
|
||||
add_setup_hook { Draper::ViewContext.infect!(self) }
|
||||
end
|
||||
end
|
||||
|
||||
MiniTest::Spec.register_spec_type(MiniTest::Spec::Decorator) do |desc|
|
||||
desc.superclass == Draper::Base
|
||||
end
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
require 'draper/test/view_context'
|
||||
|
||||
module Draper
|
||||
module DecoratorExampleGroup
|
||||
extend ActiveSupport::Concern
|
||||
|
@ -13,10 +11,6 @@ RSpec.configure do |config|
|
|||
:file_path => /spec[\\\/]decorators/
|
||||
}
|
||||
|
||||
# Specs tagged type: :decorator set the Draper view context
|
||||
config.before :type => :decorator do
|
||||
Draper::ViewContext.infect!(self)
|
||||
end
|
||||
end
|
||||
|
||||
if defined?(Capybara)
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
module Draper
|
||||
module ViewContext
|
||||
def self.infect!(context)
|
||||
context.instance_eval do
|
||||
ApplicationController.new.view_context
|
||||
Draper::ViewContext.current.controller.request ||= ActionController::TestRequest.new
|
||||
Draper::ViewContext.current.request ||= Draper::ViewContext.current.controller.request
|
||||
Draper::ViewContext.current.params ||= {}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +1,12 @@
|
|||
module Draper
|
||||
module ViewContext
|
||||
def self.current
|
||||
Thread.current[:current_view_context] || ApplicationController.new.view_context
|
||||
Thread.current[:current_view_context].tap do |context|
|
||||
context ||= ApplicationController.new.view_context
|
||||
context.controller.request ||= ActionController::TestRequest.new
|
||||
context.request ||= context.controller.request
|
||||
context.params ||= {}
|
||||
end
|
||||
end
|
||||
|
||||
def self.current=(input)
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Draper::HelperSupport do
|
||||
let(:product){ Product.new }
|
||||
|
||||
context '#decorate' do
|
||||
it 'renders a block' do
|
||||
output = ApplicationController.decorate(product){|p| p.model.object_id }
|
||||
output.should == product.object_id
|
||||
end
|
||||
|
||||
it 'uses #capture so Rails only renders the content once' do
|
||||
ApplicationController.decorate(product){|p| p.model.object_id }
|
||||
ApplicationController.capture_triggered.should be
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,30 +0,0 @@
|
|||
require 'spec_helper'
|
||||
require 'draper/test/view_context'
|
||||
|
||||
describe Draper::ViewContext do
|
||||
before(:each) do
|
||||
Thread.current[:current_view_context] = nil
|
||||
end
|
||||
|
||||
let(:app_controller) { ApplicationController }
|
||||
let(:app_controller_instance) { app_controller.new }
|
||||
|
||||
it "provides a method to create a view context while testing" do
|
||||
Draper::ViewContext.should respond_to(:infect!)
|
||||
end
|
||||
|
||||
it "copies the controller's view context to draper" do
|
||||
ctx = app_controller_instance.view_context
|
||||
Draper::ViewContext.current.should == ctx
|
||||
end
|
||||
|
||||
describe "view_context priming" do
|
||||
let(:app_controller_instance) { double(ApplicationController, :view_context => view_context) }
|
||||
let(:view_context) { double("ApplicationController#view_context") }
|
||||
|
||||
it "primes the view_context when nil" do
|
||||
app_controller.should_receive(:new).and_return(app_controller_instance)
|
||||
Draper::ViewContext.current.should == view_context
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,8 +3,6 @@ require 'ammeter/init'
|
|||
require 'rails'
|
||||
|
||||
require 'action_view'
|
||||
require './spec/support/samples/application_controller'
|
||||
|
||||
Bundler.require
|
||||
|
||||
require './spec/support/samples/active_model'
|
||||
|
@ -29,6 +27,9 @@ require './spec/support/samples/some_thing_decorator'
|
|||
require './spec/support/samples/widget'
|
||||
require './spec/support/samples/widget_decorator'
|
||||
|
||||
require 'action_controller'
|
||||
require 'action_controller/test_case'
|
||||
|
||||
module ActionController
|
||||
class Base
|
||||
Draper::System.setup(self)
|
||||
|
@ -39,3 +40,14 @@ module ActiveRecord
|
|||
class Relation
|
||||
end
|
||||
end
|
||||
|
||||
require './spec/support/samples/application_helper'
|
||||
class ApplicationController < ActionController::Base
|
||||
include ApplicationHelper
|
||||
helper_method :hello_world
|
||||
end
|
||||
|
||||
|
||||
class << Rails
|
||||
undef application # Avoid silly Rails bug: https://github.com/rails/rails/pull/6429
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue