rspec在显示页面上失败,可以无错误地访问

时间:2011-10-20 15:34:22

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

这是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

客户控制器中的“show”是:

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

有关错误的任何想法?感谢。

1 个答案:

答案 0 :(得分:0)

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

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

这是因为当something.namesomething时,您正在呼叫nil