render_template compatible with assert_template, closes #21

This commit is contained in:
Vasiliy Ermolovich 2011-10-15 14:33:51 +03:00
parent 293b8aab53
commit 16eb4aa78d
5 changed files with 70 additions and 15 deletions

View File

@ -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=>"../"

View File

@ -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=>"../"

View File

@ -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

View File

@ -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 }

View File

@ -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