26 Mar 2009, 17:37
Greg_pragsmall

Gregory Moeck (7 posts)

It seems like have_selector isn’t functioning quite right in my code, but I’m sure it’s something that I’m doing. The following is one of my tests:


it "should render a form to update the user" do
  render "users/_user.html.erb", :locals => {:button_name => "Update", :user => @user}
  response.should have_selector("form[method=post]", :action => user_path(@user)) do |form|
    form.should have_selector("input", :type => "submit")
  end
end

@user is defined above, and is loading fine. I then get the following output:

'users/_user.html.erb when user is an existing user should render a form to update the user' FAILED
expected following output to contain a <input type='submit'/> tag:
<form action="/users/1002" class="edit_user" id="edit_user_1002" method="post">
<div style="margin:0;padding:0"><input name="_method" type="hidden" value="put"></div>
<p><label for="user_first_name">First Name</label>: <input id="user_first_name" name="user[first_name]" size="30" type="text" value='#[User:0x..fdb7aeecc @name="User_1002"]'></p>
<p><label for="user_last_name">Last Name</label>: <input id="user_last_name" name="user[last_name]" size="30" type="text" value='#[User:0x..fdb7aeecc @name="User_1002"]'></p>
<p><label for="user_email">E-Mail</label>: <input id="user_email" name="user[email]" size="30" type="text" value='#[User:0x..fdb7aeecc @name="User_1002"]'></p>
<p><label for="user_login">Username</label>:

    #[User:0x..fdb7aeecc @name="User_1002"]

</p>
<p><label for="user_password">Password</label>: <input id="user_password" name="user[password]" size="30" type="password" value='#[User:0x..fdb7aeecc @name="User_1002"]'></p>
<p><label for="user_password_confirmation">Retype Password</label>: <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" value='#[User:0x..fdb7aeecc @name="User_1002"]'></p>

<p><input id="user_submit" name="commit" type="submit" value="Update"></p>
</form>

What seems weird to me is that the submit input clearly is there, but it isn’t recognizing it? Any help would be appreciated.

26 Mar 2009, 17:39
Greg_pragsmall

Gregory Moeck (7 posts)

This is lifted almost exactly from the code on page 240. When I change it to be have_tag by the way, it does pass.

27 Mar 2009, 09:33
Generic-user-small

Vladimir Klyushnikov (1 post)

This is an interesting question for me too. I’ve read a book chapter, tried have_selector and got the same issue.
Seems that nested have_selector calls select only direct children of parent tag. May be this behaviour is correct. But it does not allow to check semantics – because I should be able to wrap in any number of <div> or <p> while styling layout, and this should not break a spec.

However there is a trick that will work:


   response.should have_selector('form') do |form|
     form.inner_html.should have_selector('input')
   end 


Seems a bit verbose, but there is no other way to spec view semantics with have_selector.

11 Apr 2009, 16:23
Generic-user-small

Martin Stingl (16 posts)

I ran in a similar problem. But I’m not shure if it’s a rspec problem. I use haml. My page renders to correct xhtml code. But when I’m testing a partial (as described on chapter 20.4) I get a similar error message (see http://pastie.org/443746). But the error message told me rspec is looking for (with trailing slash) and only found (without trailing slash). Maybe it’s a haml problem?

When using the above mentioned form.inner_html.should way, the test passes.

When running the test with the debugger and I inspect the response the body contains the xhtml input-tag. So I’m really confused about the output rspec gives when failing the test …

11 Apr 2009, 21:51
Generic-user-small

Martin Stingl (16 posts)

I digged in a little bit deeper. So it seems to me the problem is caused by the to_xhtml method of Nokogiri. When using libxml2 version 2.6.* the trailing slash is missing (see http://rubyforge.org/pipermail/nokogiri-talk/20…). When using version 2.7.* to_xhtml produces the way of correct xhtml. But this doesn’t help because rspec (or webrat?) expects the short version.

05 May 2009, 18:10
Generic-user-small

Thomas Jack (1 post)

This is a webrat bug.
See https://webrat.lighthouseapp.com/projects/10503… and https://webrat.lighthouseapp.com/projects/10503…

https://webrat.lighthouseapp.com/projects/10503… is another interesting have_selector bug to watch out for.

Also, never trust the failure messages for have_selector. This is generated separately from the matching code, and is often wrong. It’ll say, for example, that it was looking for <tag#id>, when in fact it’s doing the right thing and looking for <tag id="id">.

  You must be logged in to comment