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

poseid mulder.patrick at gmail.com
Sun Jan 16 04:46:40 EST 2011


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

      rendered.should contain("photo_url")

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
         undefined method `photo' for nil:NilClass
       # ./app/views/main/_design.html.erb:3:in
       # ./app/views/main/index.html.erb:25:in
       # ./spec/views/main_spec.rb:7:in `block (2 levels) in <top

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:


     <%= render "design", :locals => { :designs => @designs } %>

What am I missing.
Thank you for your help!

More information about the rspec-users mailing list