Rspec2 部分视图给出 nil:NilClass。为什么?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题不久前就已经解决了。
牢记“约定优于配置”非常重要。
在这种情况下,可以通过将规范文件精确命名为 /app 文件夹中的文件名并在末尾添加 _spec.rb 来使规范正常工作。
因此, main 中索引 erb 的正确规范是
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