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

-- 
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to