How to write view specs with multiple forms

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.

On Nov 8, 2011, at 12:53 AM, emadridm wrote:

.field
= f.password_field :password

=> “Sign in”)
assign(:user, user)
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?

You’ve got to scope the forms so it can find both. Something like:

.sign-in-form
= form_for(:session, :url => sessions_path) do |f|

.sign-up-form
= form_for(@user) do |f|

Now the spec can be more specific:

rendered.should have_selector(".sign-in-form form",

rendered.should have_selector(".sign-up-form form",

HTH,
David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs