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

Posted by Gordon (Guest)
on 2011-10-19 07:47
(Received via mailing list)
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  -------------

<h1>New brand</h1>

<%= render 'form' %>

<%= link_to 'Back', brands_path %>


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

<h1>New brand</h1>

<%= 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? %>
    <div id="error_explanation">
      <h2><%= pluralize(@brand.errors.count, "error") %> prohibited
this brand from being saved:</h2>

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

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% 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 <input type='submit'/>
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 ---------------------

<form accept-charset="UTF-8" action="/brands" class="new_brand"
id="new_brand" method="post"><div style="margin:0;padding:
0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /
><input name="authenticity_token" type="hidden" value="hei0qtUuaZ7WcR/M
+L97FURrdb+0a+N3szt64f7HrHk=" /></div>

  <div class="field">
    <label for="brand_name">Name</label><br />
    <input id="brand_name" name="brand[name]" size="30" type="text" />
  </div>
  <div class="field">
    <label for="brand_description">Description</label><br />
    <input id="brand_description" name="brand[description]" size="30"
type="text" />
  </div>
  <div class="actions">
    <input id="brand_submit" name="commit" type="submit" value="Create
Brand" />
  </div>
</form>

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


What am I missing?

thanks :)
Posted by Gordon (Guest)
on 2011-10-19 09:40
(Received via mailing list)
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 :)

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 :)


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


Case closed :)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.