Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

render_template compatible with assert_template, closes #21

  • Loading branch information...
commit 16eb4aa78da21a3b24d191cccb2600941d1507d8 1 parent 293b8aa
Vasiliy Ermolovich nashby authored
6 gemfiles/3.0.gemfile
View
@@ -3,12 +3,12 @@
source "http://rubygems.org"
gem "rake", "~> 0.9.2"
-gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby
gem "sqlite3", :platform=>:ruby
-gem "jruby-openssl", :platform=>:jruby
-gem "shoulda-context", "~> 1.0.0.beta1"
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 "rails", "3.0.10"
gemspec :path=>"../"
12 gemfiles/3.1.gemfile
View
@@ -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 "rails", "3.1.0"
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 "coffee-rails"
+gem "sass-rails"
gemspec :path=>"../"
21 lib/shoulda/matchers/action_controller/render_template_matcher.rb
View
@@ -7,14 +7,25 @@ module ActionController # :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 @@ def in_context(context)
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
42 spec/shoulda/action_controller/render_template_matcher_spec.rb
View
@@ -25,6 +25,48 @@
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 }
2  spec/support/model_builder.rb
View
@@ -89,6 +89,7 @@ class << self
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 @@ def build_response(opts = {}, &block)
end
create_view("examples/#{action}.html.erb", "abc")
+ create_view("examples/#{partial}.html.erb", "partial")
klass.view_paths = [TMP_VIEW_PATH]
@controller = klass.new
Please sign in to comment.
Something went wrong with that request. Please try again.