Skip to content

Commit

Permalink
Merge pull request thoughtbot#23 from nashby/master
Browse files Browse the repository at this point in the history
add compatibility with assert_template, closes thoughtbot#21
  • Loading branch information
Dan Croak committed Oct 15, 2011
2 parents 293b8aa + 16eb4aa commit 1032e9d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
6 changes: 3 additions & 3 deletions gemfiles/3.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 6 additions & 6 deletions gemfiles/3.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 16 additions & 5 deletions lib/shoulda/matchers/action_controller/render_template_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions spec/shoulda/action_controller/render_template_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions spec/support/model_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand Down

0 comments on commit 1032e9d

Please sign in to comment.