On 3 May 2011, at 15:05, Matt S. wrote: > Chris Mear wrote in post #996234: >> On 2 May 2011 00:58, Matt S. <li...@ruby-forum.com> wrote: >>> accepts_nested_attributes_for.) >>> before(:each) do >>> end >>> <%= f.fields_for :owner do |owner_fields| %> >>> >>> class AssetController < ApplicationController >>> def new >>> @asset = Asset.new >>> @asset.build_owner >>> end >>> end >> >> Can you get the contents of 'response.body' from inside that spec >> example, so we can see what's being rendered when the spec is run? >> >> Chris > > Thanks Chis, > > Here is the rendered content: > > <h1>New asset</h1> > > <form accept-charset="UTF-8" action="/assets" class="new_asset" > id="new_asset" method="post"><div > style="margin:0;padding:0;display:inline"><input name="utf8" > type="hidden" value="✓" /></div> > > <div class="field"> > <label for="asset_name">Name</label><br /> > <input id="asset_name" name="asset[name]" size="30" type="text" > value="Asset_#<RSpec::Core::ExampleGroup::Nested_2:0x00000004217d08>" > /> > </div> > > <h2>Owner</h2> > > <div class="actions"> > <input id="asset_submit" name="commit" type="submit" value="Create > Asset" /> > </div> > </form> > > <a href="/assets">Back</a> > > > As you can see nothing comes up for the fields_for :owner block. I have > tried stubbing owner_attributes and owner_attributes=, as your > previously suggested, but I saw no change. It does seem that I need to > mock something else on the assets model to get this to work. I have > started combing through the the action_view source code but have not run > into anything to get me on the right trail yet, although I have learned > a lot about other things! > > If you want to see everything you can do a 'git clone > git://github.com/matthewcalebsmith/asset_owner.git' to get the whole > mock project, in case I have left out any pertinent details.
The following patch makes the example pass for me. The key change was to take out the #as_null_object call on your mock models. That seems to be interfering with Action View's detection of nested attributes acceptance, but I'm not sure exactly why. Chris >From 0829776dd45b3a0c57134f7af5d4f21feebe5fd7 Mon Sep 17 00:00:00 2001 From: Chris Mear <ch...@feedmechocolate.com> Date: Tue, 3 May 2011 18:11:40 +0100 Subject: [PATCH] Fix stubbing for nested form in assets/new. --- spec/views/assets/new.html.erb_spec.rb | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/views/assets/new.html.erb_spec.rb b/spec/views/assets/new.html.erb_spec.rb index b8845fe..9bf3b26 100644 --- a/spec/views/assets/new.html.erb_spec.rb +++ b/spec/views/assets/new.html.erb_spec.rb @@ -1,13 +1,18 @@ require 'spec_helper' describe "assets/new.html.erb" do - let(:asset) { mock_model("Asset").as_new_record.as_null_object } - let(:owner) { mock_model("Owner").as_new_record.as_null_object } + let(:asset) { mock_model("Asset").as_new_record } + let(:owner) { mock_model("Owner").as_new_record } before(:each) do asset.stub(:owner => owner) - asset.accepts_nested_attributes_for :owner + asset.stub(:name) + asset.stub(:owner_attributes=) + + owner.stub(:name) + assign(:asset, asset) + end it "renders new asset form with owner attributes" do -- 1.7.4.4 _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users