Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capybara and have_selector #190

Closed
rohitarondekar opened this issue Sep 1, 2010 · 7 comments
Closed

Capybara and have_selector #190

rohitarondekar opened this issue Sep 1, 2010 · 7 comments

Comments

@rohitarondekar
Copy link

I get the following failure:

Failures:
1) LayoutLinks should have a Home Page at '/'
   Failure/Error: response.should have_selector('title', :content => "Home")
   undefined method `has_selector?' for #<ActionDispatch::TestResponse:0x000000022057e0>
   # ./spec/requests/layout_links_spec.rb:6:in `block (2 levels) in <top (required)>'

With the following request spec:

describe "LayoutLinks" do
  it "should have a Home Page at '/'" do
    get '/'
    response.should have_selector('title', :content => "Home")
  end
 #other specs follow

I haven't tried with webrat but shouldn't capybara work?
I have gem 'capybara' in the Gemfile, is there any other configuration that I should do?

Using Rails 3.0.0 on Ruby 1.9.2-p0

@dchelimsky
Copy link
Contributor

The generators only support webrat at this time. Capybara and webrat have different APIs, so you have to change have_selector to have_css. Everything else should work ok though

@rohitarondekar
Copy link
Author

Thank you! :)

@dchelimsky
Copy link
Contributor

Does everything else work?

@rohitarondekar
Copy link
Author

Actually no it doesn't work. I get a similar error:
1) LayoutLinks should have a Home Page at '/'
Failure/Error: response.should have_css("title")
undefined method has_css?' for #<ActionDispatch::TestResponse:0x000000035b7248> # ./spec/requests/layout_links_spec.rb:6:inblock (2 levels) in <top (required)>'

for

response.should have_css("title", :content => "Home")

I also tried have_content("home") but the same error:

1) LayoutLinks should have a Home Page at '/'
   Failure/Error: response.should have_content("home")
   undefined method `has_content?' for #<ActionDispatch::TestResponse:0x000000034be648>
   # ./spec/requests/layout_links_spec.rb:6:in `block (2 levels) in <top (required)>'

@rohitarondekar
Copy link
Author

Just checked Capybara docs, and it doesn't really have an equivalent I think. It seems to favor XPATH. So I need to use have_xpath()

However this throws a similar error:

1) LayoutLinks should have a Home Page at '/'
   Failure/Error: response.should have_xpath(".//title", :text => "Home")
   undefined method `has_xpath?' for #<ActionDispatch::TestResponse:0x00000000aa0e88>
   # ./spec/requests/layout_links_spec.rb:6:in `block (2 levels) in <top (required)>'

So after some more digging I found that this works:

it "should have a Home Page at '/'" do
  visit '/'
  page.should have_xpath(".//title", :text => "Home")
end

But this looks like a integration test, and I was looking for checking the output in a controller test, is there way I can use get '/' followed by response.should have_xpath() ?

I'm sorry if I'm asking silly questions, but I can't seem to find any particular information on this.

@rohitarondekar
Copy link
Author

Wait, if I'm using capybara instead of webrat, then I should be using it's methods? I thought get() was provided by RSpec. Sorry, RSpec newbie here.

@dchelimsky
Copy link
Contributor

get() is provided by Rails. The problem is that capybara doens't support view specs. You can use it in controller and request specs, but not view specs. Webrat works well in all but request specs right now. So part of the choice of which tool to choose depends on what types of specs you want to write.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants