Skip to content

assert_template with :locals raises NoMethodError: undefined method `[]' for nil:NilClass #8516

Closed
sujoyg opened this Issue Dec 14, 2012 · 5 comments

3 participants

@sujoyg
sujoyg commented Dec 14, 2012

Observation

Consider a view such as the following:

foo/example.html.haml:

...
render :partial => 'foo/bar'
render :partial => 'foo/baz', :locals => {:var => 10}
...

The view test is as follows:

foo/example.html.haml_spec.rb:

...
it 'should render foo/bar.' do
  render
  # This works. An underscore *must* be used before partial name.
  assert_template :partial => 'foo/_bar' 
end

it 'should render foo/baz.' do
  render

  # This does *not* work, and raises NoMethodError: undefined method `[]' for nil:NilClass
  assert_template :partial => 'foo/_baz', :locals => {:var => 10}  

  # This *does* work, the difference being that
  # an underscore *must not* be used before the partial name.
  assert_template :partial => 'foo/baz', :locals => {:var => 10}  
end
...

Expectation

Partial name should be specified in the same way regardless of whether locals are specified in assert_template or not.

@senny
Ruby on Rails member
senny commented Dec 15, 2012

I'll look into this.

@senny senny was assigned Dec 15, 2012
@sujoyg
sujoyg commented Dec 15, 2012

I believe the problem is at https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb#L129.

The substitution of '_' is only happening at the beginning of the template name, whereas it should be done globally at the beginning of any segment of the template name.

@senny
Ruby on Rails member
senny commented Dec 15, 2012

@sujoyg exactly I just opened a PR to make it work and also improved the error message: #8520

@sujoyg
sujoyg commented Dec 15, 2012

Thanks @senny.

@carlosantoniodasilva
Ruby on Rails member

Fixed in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.