Rspec2 局部视图给出 nil:NilClass.为什么? [英] Rspec2 partial view gives nil:NilClass. Why?

查看:50
本文介绍了Rspec2 局部视图给出 nil:NilClass.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在我的规范中,我定义:

<前>描述main/index.html.erb"做它在产品部分显示照片网址"做分配(:设计,[stub_model(设计,:名称=>测试",:照片=>照片网址")])使成为渲染.应该包含(photo_url")结尾结尾

当我跑步时:

rspec spec/view/main_spec.rb

我收到此错误:

<前>1) main/index.html.erb 在产品部分显示照片网址失败/错误:渲染动作视图::模板::错误:nil:NilClass 的未定义方法`photo'# ./app/views/main/_design.html.erb:3:在`_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:

<前>---> 通过---> 失败

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

main.html.erb

<前>{ :designs => @designs } %>

社区怎么看?感谢您的帮助!

解决方案

问题已经解决了一段时间.

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

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

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!

解决方案

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

这篇关于Rspec2 局部视图给出 nil:NilClass.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆