Rspec2 部分视图给出 nil:NilClass。为什么?

发布于 2024-10-12 06:04:57 字数 1226 浏览 2 评论 0原文

我正在尝试使用 Rspec2 和 Rails3 来开发视图组件。 但是,我进行了以下观察,我不明白发生了什么以及如何解决这个问题。

在我的规范中,我定义:

  describe "main/index.html.erb" do
    it "displays a photo url in products partial" do
      assign(:designs, [stub_model(Design, :name => "test", :photo => "photo_url")])

      render
      rendered.should contain("photo_url")
    end
  end

当我运行时:

rspec spec/view/main_spec.rb

我收到此错误:

    1) main/index.html.erb displays a photo url in products partial
       Failure/Error: render
       ActionView::Template::Error:
         undefined method `photo' for nil:NilClass
       # ./app/views/main/_design.html.erb:3:in   `_app_views_main__design_html_erb__2937334847274155273_2170841960__1566661024965846011'
       # ./app/views/main/index.html.erb:25:in `_app_views_main_index_html_erb__837234277009287876_2170861440__898201527838028543'
       # ./spec/views/main_spec.rb:7:in `block (2 levels) in '

但是,如果我仅在我的部分中“访问”本地照片对象,则一切都会通过。这是:

在 _design.html.erb 中:

        ---> PASS
        ---> FAIL

在我看来,我将我的部分称为如下:

main.html.erb

      { :designs => @designs } %> 

社区有何想法? 感谢您的帮助!

I am trying to get going with development of view components with Rspec2 and Rails3.
However, I make the following observation, and I don't understand what is going on, and how to fix this.

In my spec I define:

  describe "main/index.html.erb" do
    it "displays a photo url in products partial" do
      assign(:designs, [stub_model(Design, :name => "test", :photo => "photo_url")])

      render
      rendered.should contain("photo_url")
    end
  end

When I run:

rspec spec/view/main_spec.rb

I get this error:

    1) main/index.html.erb displays a photo url in products partial
       Failure/Error: render
       ActionView::Template::Error:
         undefined method `photo' for nil:NilClass
       # ./app/views/main/_design.html.erb:3:in   `_app_views_main__design_html_erb__2937334847274155273_2170841960__1566661024965846011'
       # ./app/views/main/index.html.erb:25:in `_app_views_main_index_html_erb__837234277009287876_2170861440__898201527838028543'
       # ./spec/views/main_spec.rb:7:in `block (2 levels) in '

However, if I only 'access' a local photo object in my partial everything passes. This is:

in _design.html.erb:

        ---> PASS
        ---> FAIL

In my view I call my partial as follows:

main.html.erb

      { :designs => @designs } %> 

What does the community think?
Thank you for your help!

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

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

发布评论

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

评论(1

Spring初心 2024-10-19 06:04:57

这个问题不久前就已经解决了。

牢记“约定优于配置”非常重要。
在这种情况下,可以通过将规范文件精确命名为 /app 文件夹中的文件名并在末尾添加 _spec.rb 来使规范正常工作。
因此, main 中索引 erb 的正确规范是

/spec/views/main/index.erb_spec.rb

The problem has been solved some while ago already.

It is important to keep "convention over configuration" in mind.
In this case, the spec could be made working by naming the spec files exactly to the filenames in the /app folder and putting a _spec.rb at the end.
So, the right spec for the index erb in main would be

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