Skip to content

Commit

Permalink
Prevent leaking view contexts between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haines committed Jan 19, 2013
1 parent a039671 commit 3d07cb3
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/draper/test/rspec_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ module DecoratorExampleGroup

RSpec.configure do |config|
config.include DecoratorExampleGroup, example_group: {file_path: %r{spec/decorators}}, type: :decorator

[:decorator, :controller, :mailer].each do |type|
config.after(:each, type: type) { Draper::ViewContext.clear! }
end
end
end
20 changes: 20 additions & 0 deletions lib/draper/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ module Draper
end

class TestCase < active_support_test_case
module ViewContextTeardown
def teardown
super
Draper::ViewContext.clear!
end
end

module Behavior
if defined?(::Devise)
require 'draper/test/devise_helper'
Expand All @@ -29,5 +36,18 @@ module Behavior
end

include Behavior
include ViewContextTeardown
end
end

if defined?(ActionController::TestCase)
class ActionController::TestCase
include Draper::TestCase::ViewContextTeardown
end
end

if defined?(ActionMailer::TestCase)
class ActionMailer::TestCase
include Draper::TestCase::ViewContextTeardown
end
end
3 changes: 3 additions & 0 deletions spec/dummy/app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<dt>Environment:</dt>
<dd id="environment"><%= Rails.env %></dd>

<dt>Draper view context controller:</dt>
<dd id="controller"><%= Draper::ViewContext.current.controller.class %></dd>

<dt>Posted:</dt>
<dd id="posted_date"><%= post.posted_date %></dd>

Expand Down
16 changes: 9 additions & 7 deletions spec/dummy/lib/tasks/test.rake
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require 'rspec/core/rake_task'
require 'rake/testtask'
require 'rspec/core/rake_task'

Rake::Task[:test].clear
Rake::TestTask.new :test do |t|
t.libs << "test"
t.pattern = "test/**/*_test.rb"
end

RSpec::Core::RakeTask.new :rspec
RSpec::Core::RakeTask.new :spec

RSpec::Core::RakeTask.new :fast_spec do |t|
t.pattern = "fast_spec/**/*_spec.rb"
end

Rake::TestTask.new :mini_test do |t|
t.test_files = ["mini_test/mini_test_integration_test.rb"]
end

task :default => [:rspec, :mini_test, :fast_spec]
task :default => [:test, :spec, :fast_spec]
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
describe "A spec in this folder" do
it "is a decorator spec" do
expect(example.metadata[:type]).to be :decorator
end
end
require 'spec_helper'

describe "A decorator spec" do
it "can access helpers through `helper`" do
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/spec/decorators/post_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@
it "serializes overriden attributes" do
expect(decorator.serializable_hash["updated_at"]).to be :overridden
end

it "uses a test view context from ApplicationController" do
expect(Draper::ViewContext.current.controller).to be_an ApplicationController
end
end
7 changes: 7 additions & 0 deletions spec/dummy/spec/decorators/spec_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

describe "A spec in this folder" do
it "is a decorator spec" do
expect(example.metadata[:type]).to be :decorator
end
end
22 changes: 22 additions & 0 deletions spec/dummy/spec/decorators/view_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

def it_does_not_leak_view_context
2.times do
it "has an independent view context" do
expect(Draper::ViewContext.current).not_to be :leaked
Draper::ViewContext.current = :leaked
end
end
end

describe "A decorator spec", type: :decorator do
it_does_not_leak_view_context
end

describe "A controller spec", type: :controller do
it_does_not_leak_view_context
end

describe "A mailer spec", type: :mailer do
it_does_not_leak_view_context
end
4 changes: 4 additions & 0 deletions spec/dummy/spec/mailers/post_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
it "can use url helpers with an id" do
expect(email_body).to have_css "#url_with_id", text: "http://www.example.com:12345/en/posts/#{post.id}"
end

it "uses the correct view context controller" do
expect(email_body).to have_css "#controller", text: "PostMailer"
end
end
end
15 changes: 15 additions & 0 deletions spec/dummy/test/decorators/minitest/helpers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'minitest_helper'

describe "A decorator test" do
it "can access helpers through `helper`" do
assert_equal "<p>Help!</p>", helper.content_tag(:p, "Help!")
end

it "can access helpers through `helpers`" do
assert_equal "<p>Help!</p>", helpers.content_tag(:p, "Help!")
end

it "can access helpers through `h`" do
assert_equal "<p>Help!</p>", h.content_tag(:p, "Help!")
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/autorun'
require 'minitest/rails'
require 'minitest_helper'

def it_is_a_decorator_test
it "is a decorator test" do
Expand Down Expand Up @@ -33,6 +31,14 @@ def it_is_not_a_decorator_test
it_is_a_decorator_test
end

describe "Any decorator" do
it_is_a_decorator_test
end

describe "AnyDecoratorTest" do
it_is_a_decorator_test
end

describe "Any decorator test" do
it_is_a_decorator_test
end
Expand Down
24 changes: 24 additions & 0 deletions spec/dummy/test/decorators/minitest/view_context_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'minitest_helper'

def it_does_not_leak_view_context
2.times do
it "has an independent view context" do
refute_equal :leaked, Draper::ViewContext.current
Draper::ViewContext.current = :leaked
end
end
end

describe "A decorator test" do
it_does_not_leak_view_context
end

describe "A controller test" do
tests Class.new(ActionController::Base)

it_does_not_leak_view_context
end

describe "A mailer test" do
it_does_not_leak_view_context
end
15 changes: 15 additions & 0 deletions spec/dummy/test/decorators/test_unit/helpers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'test_helper'

class HelpersTest < Draper::TestCase
def test_access_helpers_through_helper
assert_equal "<p>Help!</p>", helper.content_tag(:p, "Help!")
end

def test_access_helpers_through_helpers
assert_equal "<p>Help!</p>", helpers.content_tag(:p, "Help!")
end

def test_access_helpers_through_h
assert_equal "<p>Help!</p>", h.content_tag(:p, "Help!")
end
end
24 changes: 24 additions & 0 deletions spec/dummy/test/decorators/test_unit/view_context_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'test_helper'

def it_does_not_leak_view_context
2.times do |n|
define_method("test_has_independent_view_context_#{n}") do
refute_equal :leaked, Draper::ViewContext.current
Draper::ViewContext.current = :leaked
end
end
end

class DecoratorTest < Draper::TestCase
it_does_not_leak_view_context
end

class ControllerTest < ActionController::TestCase
tests Class.new(ActionController::Base)

it_does_not_leak_view_context
end

class MailerTest < ActionMailer::TestCase
it_does_not_leak_view_context
end
4 changes: 4 additions & 0 deletions spec/dummy/test/minitest_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/autorun'
require 'minitest/rails'
3 changes: 3 additions & 0 deletions spec/dummy/test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
10 changes: 9 additions & 1 deletion spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
require 'support/matchers/have_text'

app = DummyApp.new(ENV["RAILS_ENV"])
spec_types = {
view: ["/posts/1", "PostsController"],
mailer: ["/posts/1/mail", "PostMailer"]
}

app.start_server do
{view: "/posts/1", mailer: "/posts/1/mail"}.each do |type, path|
spec_types.each do |type, (path, controller)|
page = app.get(path)

describe "in a #{type}" do
it "runs in the correct environment" do
expect(page).to have_text(app.environment).in("#environment")
end

it "uses the correct view context controller" do
expect(page).to have_text(controller).in("#controller")
end

it "can use built-in helpers" do
expect(page).to have_text("Once upon a...").in("#truncated")
end
Expand Down

0 comments on commit 3d07cb3

Please sign in to comment.