开发者

The have_selector fails in an RSpec test but page renders correctly and the tag is present

开发者 https://www.devze.com 2023-02-16 09:20 出处:网络
I\'m working my way through the Rails Tutorial book by Hartl and I\'m completely stuck on one of the tests. The test (right from the book) is very simple:

I'm working my way through the Rails Tutorial book by Hartl and I'm completely stuck on one of the tests. The test (right from the book) is very simple:

require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
    before(:each) do
        @user = Factory(:user)
    end
    ...
    it "should include the user's name" do
        get :show, :id => @user
        response.should have_selector('h1', :content => @user.name)
    end
end

The test fails with the following error:

UsersController GET 'show' should include the user's name Failure/Error: response.should have_selector('h1', :content => @user.name) expecte开发者_StackOverflow中文版d following output to contain a <h1>Steve T</h1> tag:...

The page renders properly in the browser. Here is a snipped from the HTML source rendered by the browser:

<section class="round">

<h1> <img alt="Steve T" class="gravatar" src="http://gravatar.com/..." /> Steve T </h1> </section>

I can even add a call to: print response.body in the controller spec and it will output the tags properly.

Any ideas on what I may be doing wrong?

UPDATE - It seems like we have identified a potentially significant item: The HTML in the rspec test failure message is missing the body tag everything inside it.

1) UsersController GET 'show' should include the user's name Failure/Error: response.should have_selector('h1', :content => @user.name) expected following output to contain a <h1>Steve T</h1> tag: <!DOCTYPE html> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Ruby on Rails Tutorial Sample App | Steve T</title> </head></html> # ./spec/controllers/users_controller_spec.rb:33:in block (3 levels) in <top (required)>'

Does anyone know why, if the page in the browser includes the <body> tags and the inner content and if print response.body shows the content, why would the error message skip it?


I had a similar problem when working through the Rails Tutorial. I forgot to include 'render_views' below the top 'describe' statement, which was causing the 'have_selector' test to fail.


My solution was in installing capybara, include in your Gemfile

group :development do
 gem 'capybara'
end

then do

bundle update
bundle install

than edit you code to

response.body.should have_selector('h1', :content => @user.name)

now run your tests. It should work


The test is failing because the h1 is wrapped around a <img /> so the content isn't correct and the test fails.

you could do something like this:

it "should include the user's name" do
    get :show, :id => @user
    response.should have_selector('h1')
    response.body.should contain(@user.name);
end


It turns out that syntax mistake in the partial that loads my style sheets was essentially commenting out all the body tag content. The question is why Firefox 3 rendered the HTML anyway. I actually solved the problem inadvertently by upgrading to Firefox 4. In Firefox 4, the body of the page didn't render and the debugging tools led me to the erroneous markup

0

精彩评论

暂无评论...
验证码 换一张
取 消