Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing links using rspec?

If I have something like <%= link_to "Help", help_path %> in the view file, how do I test the following using rspec?

  1. It goes to help_path.
  2. The content reads "Help".

I am currently using capybara.


edit

While answers below can work, I think I found a simpler way of doing this.

describe "some link on whatever page" do
  subject { page }
  before { visit whatever_path }
  it { should have_link('Help', href: help_path) }
end
like image 888
Jason Kim Avatar asked May 18 '12 07:05

Jason Kim


3 Answers

If you are using capybara then

my_link = find(:css, "a:contains('Help')")
my_link.click

and get page status, should be 200 or simply check


my_link.href

But also you could simply add routes specs to your integration specs in rspec. Like this to be always sure this routes exists

describe HelpController do

  it "should have proper routes" do
    {:get => "/halp/772/parser/18/edit" }.should be_routable
    {:post => "/halp/772/parser" }.should be_routable
    {:delete => "/halp/772/parser/18" }.should be_routable          
  end

end
like image 89
Jakub Oboza Avatar answered Oct 02 '22 20:10

Jakub Oboza


Your file could look like this:

require 'spec_helper'

feature "Help" do
  scenario "creation" do
    visit help_path                         # access your help path
    page.should have_content("Help")        # verify content
  end
end

If you need some help with testing with Capybara you could find some here: Railscasts #257 Request Specs and Capybara or Capybara repo at Github

like image 20
cristian Avatar answered Oct 02 '22 19:10

cristian


Use have_tag matcher. Here is a cheatsheet.

response.should have_tag("a[href$=#{help_path}]", :text => "Help")

P.S. Did you read rails guides?

like image 23
cutalion Avatar answered Oct 02 '22 21:10

cutalion