From 16eb4aa78da21a3b24d191cccb2600941d1507d8 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Sat, 15 Oct 2011 14:33:51 +0300 Subject: [PATCH] render_template compatible with assert_template, closes #21 --- gemfiles/3.0.gemfile | 6 +-- gemfiles/3.1.gemfile | 14 +++---- .../render_template_matcher.rb | 21 +++++++--- .../render_template_matcher_spec.rb | 42 +++++++++++++++++++ spec/support/model_builder.rb | 2 + 5 files changed, 70 insertions(+), 15 deletions(-) diff --git a/gemfiles/3.0.gemfile b/gemfiles/3.0.gemfile index 0268ac07..d9962053 100644 --- a/gemfiles/3.0.gemfile +++ b/gemfiles/3.0.gemfile @@ -3,12 +3,12 @@ source "http://rubygems.org" gem "rake", "~> 0.9.2" -gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby gem "sqlite3", :platform=>:ruby +gem "activerecord-jdbc-adapter", :platform=>:jruby +gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby +gem "jdbc-sqlite3", :platform=>:jruby gem "jruby-openssl", :platform=>:jruby gem "shoulda-context", "~> 1.0.0.beta1" -gem "activerecord-jdbc-adapter", :platform=>:jruby -gem "jdbc-sqlite3", :platform=>:jruby gem "rails", "3.0.10" gemspec :path=>"../" \ No newline at end of file diff --git a/gemfiles/3.1.gemfile b/gemfiles/3.1.gemfile index 49c1aa48..4538bada 100644 --- a/gemfiles/3.1.gemfile +++ b/gemfiles/3.1.gemfile @@ -3,16 +3,16 @@ source "http://rubygems.org" gem "rake", "~> 0.9.2" -gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby gem "sqlite3", :platform=>:ruby +gem "activerecord-jdbc-adapter", :platform=>:jruby +gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby +gem "jdbc-sqlite3", :platform=>:jruby gem "jruby-openssl", :platform=>:jruby gem "shoulda-context", "~> 1.0.0.beta1" -gem "coffee-rails" -gem "uglifier" -gem "sass-rails" -gem "activerecord-jdbc-adapter", :platform=>:jruby -gem "jdbc-sqlite3", :platform=>:jruby -gem "jquery-rails" gem "rails", "3.1.0" +gem "uglifier" +gem "jquery-rails" +gem "coffee-rails" +gem "sass-rails" gemspec :path=>"../" \ No newline at end of file diff --git a/lib/shoulda/matchers/action_controller/render_template_matcher.rb b/lib/shoulda/matchers/action_controller/render_template_matcher.rb index 70cb39a8..a0865a52 100644 --- a/lib/shoulda/matchers/action_controller/render_template_matcher.rb +++ b/lib/shoulda/matchers/action_controller/render_template_matcher.rb @@ -7,14 +7,25 @@ module Shoulda # :nodoc: # Example: # # it { should render_template(:show) } - def render_template(template) - RenderTemplateMatcher.new(template, self) + # + # assert that the "_customer" partial was rendered + # it { should render_template(:partial => '_customer') } + # + # assert that the "_customer" partial was rendered twice + # it { should render_template(:partial => '_customer', :count => 2) } + # + # assert that no partials were rendered + # it { should render_template(:partial => false) } + def render_template(options = {}, message = nil) + RenderTemplateMatcher.new(options, message, self) end class RenderTemplateMatcher # :nodoc: - def initialize(template, context) - @template = template.to_s + def initialize(options, message, context) + @options = options + @message = message + @template = options.is_a?(Hash) ? options[:partial] : options @context = context end @@ -38,7 +49,7 @@ module Shoulda # :nodoc: def renders_template? begin - @context.send(:assert_template, @template) + @context.send(:assert_template, @options, @message) @negative_failure_message = "Didn't expect to render #{@template}" true rescue Shoulda::Matchers::AssertionError => error diff --git a/spec/shoulda/action_controller/render_template_matcher_spec.rb b/spec/shoulda/action_controller/render_template_matcher_spec.rb index 16e91728..271118c2 100644 --- a/spec/shoulda/action_controller/render_template_matcher_spec.rb +++ b/spec/shoulda/action_controller/render_template_matcher_spec.rb @@ -25,6 +25,48 @@ describe Shoulda::Matchers::ActionController::RenderTemplateMatcher do end end + context "a controller that renders a partial" do + before do + @controller = build_response(:partial => '_customer') { render :partial => 'customer' } + end + + it "should accept rendering that partial" do + @controller.should render_template(:partial => '_customer') + end + + it "should reject rendering a different template" do + @controller.should_not render_template(:partial => '_client') + end + + it "should accept rendering that template in the given context" do + @controller.should render_template(:partial => '_customer').in_context(self) + end + + it "should reject rendering a different template in the given context" do + @controller.should_not render_template(:partial => '_client').in_context(self) + end + end + + context "a controller that doesn't render partials" do + before do + @controller = build_response(:action => 'show') { render } + end + + it "should not render a partial" do + @controller.should render_template(:partial => false) + end + end + + context "a controller that renders a partial several times" do + before do + @controller = build_response(:partial => '_customer') { render :partial => 'customer', :collection => [1,2] } + end + + it "should accept rendering that partial twice" do + @controller.should render_template(:partial => '_customer', :count => 2) + end + end + context "a controller that doesn't render a template" do before do @controller = build_response { render :nothing => true } diff --git a/spec/support/model_builder.rb b/spec/support/model_builder.rb index a3a06aad..55d574f9 100644 --- a/spec/support/model_builder.rb +++ b/spec/support/model_builder.rb @@ -89,6 +89,7 @@ module ModelBuilder def build_response(opts = {}, &block) action = opts[:action] || 'example' + partial = opts[:partial] || '_partial' klass = define_controller('Examples') block ||= lambda { render :nothing => true } klass.class_eval { layout false; define_method(action, &block) } @@ -97,6 +98,7 @@ module ModelBuilder end create_view("examples/#{action}.html.erb", "abc") + create_view("examples/#{partial}.html.erb", "partial") klass.view_paths = [TMP_VIEW_PATH] @controller = klass.new