RSpec:使用嵌套资源时无法将Image转换为String

时间:2010-04-29 12:41:58

标签: ruby-on-rails ruby views rspec stub

我遇到了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上定义的'polymorphic_path'。不确定这实际上是否意味着什么。

1 个答案:

答案 0 :(得分:0)

确定。这与Rspec无关,而是与正确使用嵌套资源和rails帮助程序有关。

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

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

只是错误信息无效。