-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from haines/test_integration
Add `helper` method to tests
- Loading branch information
Showing
4 changed files
with
93 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters