Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

can't pass :locals to #assert_template without a view test case (#3415) #7848

Merged
merged 1 commit into from

3 participants

@senny
Owner

the documentation on #assert_template states that the :locals option is only available in view test cases:

# In a view test case, you can also assert that specific locals are passed
# to partials:

I added a warning when it's passed in an inapropriate context to prevent a NoMethodError.

I also changed the code to simply ignore the :locals option if @locals is not set. This way the users will get a warning but the tests don't fail.

@senny
Owner

this does not fix #3415 but as far as the documentation goes the :locals option works only in a view test case.

@steveklabnik I think we can clos #3415 when this PR is merged. If the locals option should be supported outside view test cases I think we need some more changes.

@rafaelfranca what do you think?

@steveklabnik
Collaborator

This doesn't merge cleanly.

@rafaelfranca
Owner

Seems good to me.

actionpack/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Warn when the :locals option is passed to #assert_template outside of a view test case
@rafaelfranca Owner

put:

Warn when the `:locals` option is passed to `assert_template` outside of a view test case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
actionpack/test/controller/render_test.rb
@@ -1428,6 +1428,17 @@ def test_partial_collection_with_locals
assert_equal "Bonjour: davidBonjour: mary", @response.body
end
+ def test_locals_option_to_assert_template_is_not_supported
+ warning_buffer = StringIO.new
+ $stderr = warning_buffer
+
+ get :partial_collection_with_locals
+ assert_template :partial => 'customer_greeting', :locals => {:greeting => 'Bonjour'}
@rafaelfranca Owner

Use the Ruby 1.9 hash syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rafaelfranca
Owner

Put in the end commit message:

Closes #3415

remove the issue number from the first line.

And github will close it when we merge

@rafaelfranca
Owner

We need a rebase as @steveklabnik said

@senny
Owner

@rafaelfranca rebased, CHANGELOG updated, and new hash syntax used. I could not rerun, the tests because I can't bundle the dependencies on the latest version of master. Something seems to be off with the railties dependencies:

Bundler could not find compatible versions for gem "railties":
  In Gemfile:
    turbolinks (>= 0) ruby depends on
      railties (~> 3.1.0.rc1) ruby

    rails (>= 0) ruby depends on
      railties (4.0.0.beta)
@senny senny can't pass :locals to #assert_template without a view test case. Closes
#3415

the documentation on #assert_template states that the :locals option is
only available in view test cases:

    # In a view test case, you can also assert that specific locals are passed
    # to partials:

I added a warning when it's passed in an inapropriate context to prevent
a NoMethodError.
af677da
@senny
Owner

@rafaelfranca the dependencies are fine again. I rebased the branch and everything is still passing.

@rafaelfranca rafaelfranca merged commit bcbf1bb into rails:master
@rafaelfranca
Owner

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 6, 2012
  1. @senny

    can't pass :locals to #assert_template without a view test case. Closes

    senny authored
    #3415
    
    the documentation on #assert_template states that the :locals option is
    only available in view test cases:
    
        # In a view test case, you can also assert that specific locals are passed
        # to partials:
    
    I added a warning when it's passed in an inapropriate context to prevent
    a NoMethodError.
This page is out of date. Refresh to see the latest.
View
7 actionpack/CHANGELOG.md
@@ -1,6 +1,11 @@
## Rails 4.0.0 (unreleased) ##
-* The `Rack::Cache` middleware is now disabled by default. To enable it,
+* Warn when the `:locals` option is passed to `assert_template` outside of a view test case
+ Fix #3415
+
+ *Yves Senn*
+
+* The `Rack::Cache` middleware is now disabled by default. To enable it,
set `config.action_dispatch.rack_cache = true` and add `gem rack-cache` to your Gemfile.
*Guillermo Iguaran*
View
10 actionpack/lib/action_controller/test_case.rb
@@ -123,9 +123,13 @@ def assert_template(options = {}, message = nil)
if expected_partial = options[:partial]
if expected_locals = options[:locals]
- actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')]
- expected_locals.each_pair do |k,v|
- assert_equal(v, actual_locals[k])
+ if defined?(@locals)
+ actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')]
+ expected_locals.each_pair do |k,v|
+ assert_equal(v, actual_locals[k])
+ end
+ else
+ warn "the :locals option to #assert_template is only supported in a ActionView::TestCase"
end
elsif expected_count = options[:count]
actual_count = @_partials[expected_partial]
View
11 actionpack/test/controller/render_test.rb
@@ -1428,6 +1428,17 @@ def test_partial_collection_with_locals
assert_equal "Bonjour: davidBonjour: mary", @response.body
end
+ def test_locals_option_to_assert_template_is_not_supported
+ warning_buffer = StringIO.new
+ $stderr = warning_buffer
+
+ get :partial_collection_with_locals
+ assert_template partial: 'customer_greeting', locals: { greeting: 'Bonjour' }
+ assert_equal "the :locals option to #assert_template is only supported in a ActionView::TestCase\n", warning_buffer.string
+ ensure
+ $stderr = STDERR
+ end
+
def test_partial_collection_with_spacer
get :partial_collection_with_spacer
assert_equal "Hello: davidonly partialHello: mary", @response.body
Something went wrong with that request. Please try again.