Skip to content

Commit

Permalink
Added a render_template matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jun 7, 2010
1 parent 0fe568f commit 8f983a9
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/shoulda/action_controller/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'shoulda/action_controller/matchers/set_session_matcher'
require 'shoulda/action_controller/matchers/route_matcher'
require 'shoulda/action_controller/matchers/redirect_to_matcher'
require 'shoulda/action_controller/matchers/render_template_matcher'

module Shoulda # :nodoc:
module ActionController # :nodoc:
Expand Down
54 changes: 54 additions & 0 deletions lib/shoulda/action_controller/matchers/render_template_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Shoulda # :nodoc:
module ActionController # :nodoc:
module Matchers

# Ensures a controller rendered the given template.
#
# Example:
#
# it { should render_template(:show) }
def render_template(template)
RenderTemplateMatcher.new(template, self)
end

class RenderTemplateMatcher # :nodoc:

def initialize(template, context)
@template = template
@context = context
end

def matches?(controller)
@controller = controller
renders_template?
end

attr_reader :failure_message, :negative_failure_message

def description
"render template #{@template}"
end

def in_context(context)
@context = context
self
end

private

def renders_template?
begin
@context.send(:assert_template, @template)
@negative_failure_message = "Didn't expect to render #{@template}"
true
rescue Test::Unit::AssertionFailedError => error
@failure_message = error.message
false
end
end

end

end
end
end
37 changes: 37 additions & 0 deletions test/matchers/controller/render_template_matcher_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')

class RenderTemplateMatcherTest < ActionController::TestCase # :nodoc:

context "a controller that renders a template" do
setup do
@controller = build_response(:action => 'show') { render }
end

should "accept rendering that template" do
assert_accepts render_template(:show), @controller
end

should "reject rendering a different template" do
assert_rejects render_template(:index), @controller
end

should "accept rendering that template in the given context" do
assert_accepts self.class.render_template(:show).in_context(self), @controller
end

should "reject rendering a different template in the given context" do
assert_rejects self.class.render_template(:index).in_context(self), @controller
end
end

context "a controller that doesn't render a template" do
setup do
@controller = build_response { render :nothing => true }
end

should "reject rendering a template" do
assert_rejects render_template(:show), @controller
end
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class RenderWithLayoutMatcherTest < ActionController::TestCase # :nodoc:

context "a controller that renders with a layout" do
setup do
create_view('layouts/wide.html.erb', '123')
@controller = build_response { render :layout => 'wide' }
end

Expand Down
24 changes: 20 additions & 4 deletions test/model_builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class ActiveSupport::TestCase

TMP_VIEW_PATH =
File.expand_path(File.join(File.dirname(__FILE__), '..', 'rails_root', 'tmp', 'views')).freeze

def create_table(table_name, &block)
connection = ActiveRecord::Base.connection

Expand Down Expand Up @@ -60,22 +64,32 @@ def define_routes(&block)
new_routes.draw(&block)
end

def build_response(&block)
def build_response(opts = {}, &block)
action = opts[:action] || 'example'
klass = define_controller('Examples')
block ||= lambda { render :nothing => true }
klass.class_eval { define_method(:example, &block) }
klass.class_eval { define_method(action, &block) }
define_routes do |map|
map.connect 'examples', :controller => 'examples', :action => 'example'
map.connect 'examples', :controller => 'examples', :action => action
end

create_view("examples/#{action}.html.erb", "abc")
klass.view_paths = [TMP_VIEW_PATH]

@controller = klass.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
get :example
get action

@controller
end

def create_view(path, contents)
full_path = File.join(TMP_VIEW_PATH, path)
FileUtils.mkdir_p(File.dirname(full_path))
File.open(full_path, 'w') { |file| file.write(contents) }
end

def teardown_with_models
if @defined_constants
@defined_constants.each do |class_name|
Expand All @@ -99,6 +113,8 @@ def teardown_with_models
@replaced_routes.reload!
end

FileUtils.rm_rf(TMP_VIEW_PATH)

teardown_without_models
end
alias_method :teardown_without_models, :teardown
Expand Down

0 comments on commit 8f983a9

Please sign in to comment.