0

RSpecビューテストで問題が発生しました。ネストされたリソースとbelongs_toアソシエーションを持つモデルを使用しています。

これが私がこれまでに持っているものです:

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

私が受け取っているエラー:

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'

例外が発生するレールコードを見ると、あまりわかりません。ここで何が起こっているのかをどのように絞り込むことができるかについてのアイデアはありますか?

私が試した1つのことは、例から直接form_forを呼び出すことでしたが、Spec :: Rails :: Example :: ViewExampleGroup :: Subclass_4:0xblahで定義された「polymorphic_path」の欠如について別のエラーが発生しました。それが実際に何かを意味するかどうかはわかりません。

4

1 に答える 1

0

Ok。これはRspecとは何の関係もなく、ネストされたリソースとRailsヘルパーの適切な使用と関係があります。

どうやら、ビューでネストされたリソースを処理する正しい方法は次のとおりです。

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

エラーメッセージが役に立たないだけです。

于 2010-04-29T15:15:53.697 に答える