Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing links with rspec and cabybara: old and new syntax

In the old syntax of rspec I could do something like this:

It "should have the right URL" do 
  get :show, :id => @user
  response.should have_selector('td>a', :content => user_path(@user), 
                                        :href => user_path(@user))
end

In the new sintax we can do this:

It "should have the right URL" do 
 visit user_path(@user) 
 expect(page).to have_link user_path(@user), href: user_path(@user)
end

But, in the new syntax of rspec and using capybara, how can I say where the link should be? In the first example I say that it's in 'td>a', what about the second example?

Thanks!

Editing:

If I try this:

It "should have the right URL" do 
 expect(page).to have_selector('td>a', :content => user_path(@user), 
                                        :href => user_path(@user))

I get the following error:

  1) UsersController GET 'show' should have the right URL
 Failure/Error: expect(page).to have_selector(:content => user_path(@user),
 ArgumentError:
   invalid keys :content, :href, should be one of :text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait
like image 419
bpereira Avatar asked Dec 02 '25 07:12

bpereira


1 Answers

Maybe you also upgraded your capybara gem?

Try:

It "should have the right URL" do 
  expect(page).to have_selector("td>a[href='#{user_path(@user)}']", 
    :text=> user_path(@user))
like image 68
Uri Agassi Avatar answered Dec 03 '25 19:12

Uri Agassi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!