I have two models in a has_many relationship. The parent model
(Invoice) accepts the attributes for it's children (InvoiceItem).
The models and the associated controller (InvoicesController) are
absolutely standard stuff. This is the significant part of the model
code:

 class Invoice < ActiveRecord::Base
has_many :items, :class_name => "InvoiceItem", :dependent => :destroy

accepts_nested_attributes_for :items, :allow_destroy => true, :reject_if => Proc.new { |attributes| attributes['quantity'].blank? && attributes['description'].blank? && attributes['unit_price'].blank?
   }

   validates_presence_of :recipient, :recipient_street
   validates_presence_of :recipient_zipcode, :recipient_city
 end

 class InvoiceItem < ActiveRecord::Base
   belongs_to :invoice
   validates_presence_of :invoice_id
   validates_presence_of :quantity, :description, :unit_price
 end

Now, here's part of the view I'm trying to test:

 <% form_for @invoice, :html => {:multipart => true} do |f| %>
<p>text fields for recipient, recipient_street, recipient_zipcode, recipient_city and so on go here</p>
   <% f.fields_for :items do |form_item| %>
<p>text fields for quantity, description, unit_price and so on go here</p>
     <% # exception is thrown in the next line: %>
     <% unless form_item.object.new_record? %>
       <br/><%= form_item.check_box :_delete %>
       <%= form_item.label :_delete, "Remove" %>
     <% end %>
   <% end %>
 <% end %>

Everything is working just fine if i test it manually in the browser.
However, I don't get any rspec tests to work.

 describe "/invoices/_form.html.erb" do
   before do
     @items = [mock_model(InvoiceItem), mock_model(InvoiceItem)]
assigns[:invoice] = @invoice = mock_model(Invoice, :items => @items)
     render
   end
   it "renders the invoice form" do
     response.should have_tag("form")
   end
 end

When i run `rake spec:views` i get the following error (line numbers do not match with supplied sample code):

ActionView::TemplateError in '/invoices/_form.html.erb renders the invoice form'
 You have a nil object when you didn't expect it!
 You might have expected an instance of ActiveRecord::Base.
 The error occurred while evaluating nil.new_record?
 On line #3 of app/views/invoices/_form.html.erb

     1: <% form_for @invoice, :html => {:multipart => true} do |f| %>
     2:   <% f.fields_for :items do |form_item| %>
     3:     <% unless form_item.object.new_record? %>
     4:     <br/><%= form_item.check_box :_delete %>
     5:     <%= form_item.label :_delete, "Remove" %>
     6:   <% end %>

     app/views/invoices/_form.html.erb:3
     app/views/invoices/_form.html.erb:2
     app/views/invoices/_form.html.erb:1
     /spec/views/invoices/_form.html.erb_spec.rb:7
     /usr/local/lib/ruby/1.8/timeout.rb:53:in `timeout'
     vendor/plugins/rspec/bin/spec:4

So, how do i prevent this? I can't imagine that no one has tried that
before. I appreciate ANY advice or pointers greatly!

_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to