View spec for 'new' keeps failing despite generated html has expected attribute

Hi guys,

I have created a new resource, brands and here’s the spec for ‘new’
which is failing.

I do not know what’s going on despite checking it out for a few times
already

------ app/views/brands/new.html.erb - start -------------

New brand

<%= render ‘form’ %>

<%= link_to ‘Back’, brands_path %>

------ app/views/brands/new.html.erb - start -------------

New brand

<%= render ‘form’ %>

<%= link_to ‘Back’, brands_path %>

------ app/views/brands/new.html.erb - end -------------

------ app/views/brands/_form.html.erb - start -------------

<%= form_for(@brand) do |f| %>
<% if @brand.errors.any? %>


<%= pluralize(@brand.errors.count, “error”) %> prohibited
this brand from being saved:

  <ul>
  <% @brand.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>

<% end %>

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.submit %>
<% end %>

------ app/views/brands/_form.html.erb - end -------------

--------spec/views/parts/new.html.erb_spec.rb - start --------

require ‘spec_helper’

describe “brands/new.html.erb” do
include Devise::TestHelpers

before(:each) do
@brand = assign( :brand,
stub_model( Brand,
:name => ‘Apple’,
:description => ‘OS X and Ipods’
)
)
end

it “renders new brand form” do
render
rendered.should have_selector( ‘form’,
:method => ‘post’,
:action => brands_path
) do |form|
form.should have_selector(“input”, :type => “submit”)

    end

end

end

--------spec/views/parts/new.html.erb_spec.rb - end --------

My spec, spec/views/brands/new.html.erb_spec.rb fails with the
following error:

-------- Error message - start ------------------------

  1. brands/new.html.erb renders new brand form
    Failure/Error: form.should have_selector(“input”, :type =>
    “submit”)
    expected following output to contain a
    tag:

    ./spec/views/brands/new.html.erb_spec.rb:21

    ./spec/views/brands/new.html.erb_spec.rb:17

-------- Error message - end ------------------------

When I do a view source on the page, I could very well see the submit
button in the form.

----------- html form extract - start ---------------------

Name
Description

----------- html form extract - end ---------------------

What am I missing?

thanks :slight_smile:

Fixed the issue as soon as I walked off my desk.
Thought it was because of the resource object, @brand in spec/views/
parts/new.html.erb_spec.rb not being stubbed properly.

Checked and I was right :slight_smile:

So,
before(:each) do
@brand = assign( :brand,
stub_model( Brand,
:name => ‘Apple’,
:description => ‘OS X and Ipods’
)
)
end

should have also had the “.as_new_record” method there. Reading below,
my view specs pass :slight_smile:

before(:each) do
    @brand = assign( :brand,
        stub_model( Brand,
                      :name        => 'Apple',
                      :description => 'OS X and Ipods'
        ).as_new_record
    )
end

Case closed :slight_smile: