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'

查看发生异常的 rails 代码并不是很有启发性。关于如何缩小这里发生的事情的任何想法?

我尝试过的一件事是直接从示例中调用 form_for,但我得到了一个不同的错误,抱怨缺少在 Spec::Rails::Example::ViewExampleGroup::Subclass_4:0xblah 上定义的“多态路径”。不确定这是否真的意味着什么。

4

1 回答 1

0

好的。这与 Rspec 无关,与正确使用嵌套资源和 rails 助手有关。

显然,在视图中处理嵌套资源的正确方法是:

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

只是错误消息没有帮助。

于 2010-04-29T15:15:53.697 回答