views:

50

answers:

1

I'm having trouble with and RSpec view test. I'm using nested resources and the model with a belongs_to association.

Here's what I have so far:

describe "/images/edit.html.erb" do
  include ImagesHelper

  before(:each) do
    @image_pool = stub_model(ImagePool, :new_record => false,
                             :base_path => '/')
    assigns[:image] = @image =
      stub_model(Image,
                 :new_record? => false,
                 :source_name => "value for source_name",
                 :image_pool => @image_pool)
  end

  it "renders the edit image form" do
    render

    response.should have_tag("form[action=#{image_path(@image)}][method=post]") do
      with_tag('input#image_source_name[name=?]', "image[source_name]")
    end
  end
end

The error I'm receiving:

ActionView::TemplateError in '/images/edit.html.erb renders the edit image form'
can't convert Image into String
On line #3 of app/views/images/edit.html.erb

    1: <h1>Editing image</h1>
    2:
    3: <% form_for(@image) do |f| %>
    4:   <%= f.error_messages %>
    5:
    6:   <p>

    app/views/images/edit.html.erb:3
    /opt/dtcm/railstest/lib/ruby/gems/1.9.1/gems/rspec-rails-1.3.2/lib/spec/rails/extensions/action_view/base.rb:27:in `render_with_mock_proxy'
    /opt/dtcm/railstest/lib/ruby/gems/1.9.1/gems/rspec-rails-1.3.2/lib/spec/rails/example/view_example_group.rb:170:in `render'

Looking at the rails code where the exception occurs is not very revealing. Any ideas on how I can narrow down what is going on here?

One thing I tried was calling form_for directly from the example and I got a different error griping about lack of 'polymorphic_path' defined on Spec::Rails::Example::ViewExampleGroup::Subclass_4:0xblah. Not sure if that actually means anything.

A: 

Ok. This has nothing to do with Rspec and everything to do with proper use of nested resources and rails helpers.

Apparently the right way to handle nested resources in a view is:

<% form_for [@image_pool, @image] do |f| %>

It's just that the error message isn't helpful.

darrint