[rspec-users] Rspec2 partial view gives nil:NilClass. Why?

David Chelimsky dchelimsky at gmail.com
Fri Jan 21 08:21:05 EST 2011


On Jan 21, 2011, at 4:43 AM, poseid wrote:
> On Jan 16, 10:46 am, poseid <mulder.patr... at gmail.com> wrote:
>> Hello,
>> 
>> I am trying to get going with development of view components with
>> Rspec2 and Rails3.
>> However, I make the following observation, and I don't understand what
>> is going on, and how to fix this.
>> 
>> In my spec I define:
>> 
>>   describe "main/index.html.erb" do
>>     it "displays a photo url in products partial" do
>>       assign(:designs, [stub_model(Design, :name => "test", :photo =>
>> "photo_url")])
>> 
>>       render
>>       rendered.should contain("photo_url")
>>     end
>>   end
>> 
>> When I run:
>> 
>>     rspec spec/view/main_spec.rb
>> 
>> I get this error:
>> 
>>     1) main/index.html.erb displays a photo url in products partial
>>        Failure/Error: render
>>        ActionView::Template::Error:
>>          undefined method `photo' for nil:NilClass
>>        # ./app/views/main/_design.html.erb:3:in
>> `_app_views_main__design_html_erb__2937334847274155273_2170841960__1566661024965846011'
>>        # ./app/views/main/index.html.erb:25:in
>> `_app_views_main_index_html_erb__837234277009287876_2170861440__898201527838028543'
>>        # ./spec/views/main_spec.rb:7:in `block (2 levels) in <top
>> (required)>'
>> 
>> However, if I only 'access' a local photo object in my partial
>> everything passes. This is:
>> 
>> in _design.html.erb:
>> 
>>       <%= design %>  ---> PASS
>>       <%= design.photo %>  ---> FAIL
>> 
>> In my view I call my partial as follows:
>> 
>> main.html.erb
>> 
>>      <%= render "design", :locals => { :designs => @designs } %>
>> 
>> What am I missing.
>> Thank you for your help!

> The problem is probably due to assignment of the photo attachment.
> I got a unit test working with:
> 
>  test "the partial" do
>     design = Design.new :photo => File.new(Rails.root + "test/
> fixtures/images/rails.png")
>     p design.photo(:medium)
>     render :partial => "main/design", :locals => { :design =>
> design }, :layout => false
>     assert_select "div.product-img a.product-link img[src^='/designs/
> photos//medium_rails.png']"
>   end

And the same does not work in a view spec?


More information about the rspec-users mailing list