Goalie
Goalie

Reputation: 3105

Why is Rspec/Capybara giving me a expected css ... to return something error?

The test I am using is:

it { should have_link('Logout', href: logout_url) }

I am just testing for the existence of a <a> tag with Logout as the text, and the href to be logout_url path. Should I be using another syntax for this?

Gems:

rails 3.2.6
rspec-rails 2.10.1
capybara 1.1.2

Upvotes: 0

Views: 868

Answers (2)

Kevin Bedell
Kevin Bedell

Reputation: 13404

There are a number of ways you can do this. I'd recommend (if you haven't already) familiarizing yourself with the overall capybara dsl as well as some of the specific sections such as the capybara matchers.

I believe your test is fine written as:

it { should have_link('Logout', href: logout_url) }

as long as the link text really is 'Logout' and the href is specified correctly (see below).

According to the capybara docs, the first parameter to have_link is what's called the locator. In your case it's 'Logout'. This can be either the text in the link itself, or it can be the #id of the dom element. So in your case you need to have the text 'Logout' in the link that has the logout_url link.

Note that locaters in capybara are case sensitive, so make sure you match on case.

You might also want to consider whether you should be using logout_url and not logout_path. By default, rails doesn't always generate the full url for most links. It just generates paths. Here's the difference:

users_url: http://localhost/users
users_path: /users

Check your page to see which of these types of url's are being generated by your app.

Upvotes: 1

Aleksey Bakin
Aleksey Bakin

Reputation: 1536

Probably it should be logout_path.

Also you can try to use save_and_open_page to understand what's going wrong.

Upvotes: 0

Related Questions