On 2 May 2011 00:58, Matt S. <li...@ruby-forum.com> wrote:
> 1) Is there a way to make this trivial example spec to pass? (If so,
> how?)
>
> 2) Advice: I would like to write more view specs, especially on views
> for models with more complex relationships. Is it worth it?
>
> (The reason I ask this somewhat rhetorical question is because I cannot
> find any examples of this on the web and have tried about everything I
> can think of, but I still can't get view specs containing nested model
> forms to pass, using fields_for on models with
> accepts_nested_attributes_for.)
>
> Much thanks!!!
>
> Matt Smith
>
> #spec/views/assets/new.html.erb_spec.rb
> 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 }
>
>  before(:each) do
>    asset.stub(:owner => owner)
>    assign(:asset, asset)
>  end
>
>  it "renders new asset form with owner attributes" do
>    render
>    assert_select "form[method=post][action=?]", assets_path do
>      assert_select
> "input[type=text][name='asset[owner_attributes][name]']"
>    end
>  end
> end
>
> #app/views/assets/new.html.erb
> <%= form_for(@asset) do |f| %>
>
>  <div class="field">
>    <%= f.label :name %><br />
>    <%= f.text_field :name %>
>  </div>
>
>  <%= f.fields_for :owner do |owner_fields| %>
>    <div class="field">
>      <%= owner_fields.label :name %>
>      <%= owner_fields.text_field :name %>
>    </div>
>  <% end %>
>
>  <div class="actions">
>    <%= f.submit %>
>  </div>
> <% end %>
>
> # Where the relationship will be the following....
> class Asset < ActiveRecord::Base
>  has_one :owner
>  accepts_nested_attributes_for :owner
> end
>
> class Owner < ActiveRecord::Base
>  belongs_to :asset
> end
>
> 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
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to