RSpec:使用嵌套资源时无法将图像转换为字符串

发布于 2024-08-31 05:42:28 字数 1613 浏览 1 评论 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 上定义的“polymorphic_path”。不确定这是否真的意味着什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆离笙 2024-09-07 05:42:28

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

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

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

只是错误消息没有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文