Skip to content
Browse files

Added a render_template matcher

  • Loading branch information...
1 parent 0fe568f commit 8f983a9bfe4f5d85adc2ef361b1155a6b37a831b @jferris jferris committed Feb 12, 2010
View
1 lib/shoulda/action_controller/matchers.rb
@@ -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:
View
54 lib/shoulda/action_controller/matchers/render_template_matcher.rb
@@ -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
View
37 test/matchers/controller/render_template_matcher_test.rb
@@ -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
View
1 test/matchers/controller/render_with_layout_matcher_test.rb
@@ -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
View
24 test/model_builder.rb
@@ -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
@@ -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|
@@ -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

0 comments on commit 8f983a9

Please sign in to comment.
Something went wrong with that request. Please try again.