【问题标题】:RSpec: Can't convert Image to String when using Nested ResourceRSpec:使用嵌套资源时无法将图像转换为字符串
【发布时间】:2010-04-29 12:41:58
【问题描述】:

我在使用 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 上定义的“多态路径”。不确定这是否真的意味着什么。

【问题讨论】:

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


    【解决方案1】:

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

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

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

    只是错误信息没有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多