【问题标题】:rspec failed on show page which can be accessed without errorrspec 在显示页面上失败,可以正常访问
【发布时间】:2011-10-20 15:34:22
【问题描述】:

这是 rspec 错误消息:

  5) CustomersController GET customer page 'show' should be successful
     Failure/Error: get 'show' , :id => customer.id
     ActionView::Template::Error:
       undefined method `name' for nil:NilClass
     # ./app/views/customers/show.html.erb:43:in `_app_views_customers_show_html_erb___16283441_33497004'
     # ./spec/controllers/customers_controller_spec.rb:41:in `block (3 levels) in <top (required)>'

name 是客户模型中的一列。

上面的错误是由客户控制器的规范代码引起的:

it "'show' should be successful" do
  customer = Factory(:customer)
  get 'show' , :id => customer.id
  response.should be_success
end

客户控制器中的“显示”是:

  def show
    @customer = Customer.find(params[:id])
  end

对这个错误有什么想法吗?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 rspec2


    【解决方案1】:

    错误 (undefined method 'name' for nil:NilClass) 来自您的显示视图,第 43 行。

         # ./app/views/customers/show.html.erb:43:in 
    

    这是因为当somethingnil 时,您正在调用something.name

    【讨论】:

      猜你喜欢
      • 2019-05-04
      • 1970-01-01
      • 2013-11-01
      • 2010-12-28
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      相关资源
      最近更新 更多