rspec/rspec-rails — Issue #483: get 'index' failed in view file but actual render shows no problem.
Author: emclab
Created: 2012-01-16 05:04:51+00:00
State: open

Our app has a simple case for testing index in rfq controller (rails 3.1.0), it is:

it "should be successful" do
get 'index'
response.should be_success
end

we just found that the case failed because of an error in index.html.erb(it passed before though). The line of code is

<% @rfqs.each do |r| %>

<%= r.customer.short_name %>

<% end %>

The error is:

1) RfqsController 'index' should be successful
Failure/Error: get 'index'
ActionView::Template::Error:
undefined method `short_name' for nil:NilClass
# ./app/views/rfqs/index.html.erb:28:in `block in _app_views_rfqs_index_html_erb__624673492_53806644'
# c:in `each'
# c:in `each'
# ./app/views/rfqs/index.html.erb:18:in `_app_views_rfqs_index_html_erb__624673492_53806644'
# ./spec/controllers/rfqs_controller_spec.rb:16:in `block (3 levels) in '

However, the app has no problem to display the short_name in index page. Also in rails console, r.customer.short_name did return the short name from customer table.

In rfq model, it has association:

belongs_to :customer

Any thoughts about the problem? Thanks.