[rspec-users] How to write view specs with multiple forms

emadridm emadridm at gmail.com
Tue Nov 8 01:53:06 EST 2011


Hi guys,

I don't know how to write an spec for a view template with two or more
forms. The follow content corresponds to the file named home.html.haml

!!! 5
= form_for(:session, :url => sessions_path) do |f|
  .field
    = f.label :email
    = f.text_field :email
  .field
    = f.label :password
    = f.password_field :password
  .actions
    = f.submit "Sign in"
= form_for(@user) do |f|
  .field
    = f.text_field :fullname
  .field
    = f.text_field :email
  .field
    = f.password_field :password
  .actions
    = f.submit "Sign up"

The _spec file (home.html.haml_spec.rb) content is:

require 'spec_helper'

describe "pages/home.html.haml" do

  describe 'sign in form' do

    before(:each) do
      render
    end

    it 'should render a form to create a new session' do
      rendered.should have_selector("form",
                                    :method => "post",
                                    :action => sessions_path) do |
form|
        form.should have_selector("input", :type => "submit", :value
=> "Sign in")
      end
    end

  end # describe: 'sign in form'

  describe 'sign up form' do

    let(:user) {mock_model("User").as_new_record.as_null_object}

    before(:each) do
      assign(:user, user)
      render
    end

    it 'should render a form to create a new user' do
      rendered.should have_selector("form",
                                    :method => "post",
                                    :action => users_path) do |form|
        form.should have_selector("input", :type => "submit", :value
=> "Sign up")
      end
    end

  end # describe: 'sign up form'

end

As this, the second example passes and the first doesn't. Rspec shows
the message "undefined method model_name for Nilclass:Class". If I
delete the second form, the first example passes.

Anybody can tell me how to use have_selector properly?

thanks a lot.


More information about the rspec-users mailing list