mdkrog
mdkrog

Reputation: 378

Best practice for testing capybara visit page

So I was wondering what the best way is to go about testing that a page was actually visited using capybara

describe "Pages" do
  subject { page }
  describe "Home page" do
    it "should have the title 'My Home Page'" do
      visit root_path
      expect(page).to have_title('My Home Page')      
    end
  end
end

Now it seems like the standard way to test this is to compare the title of the page (as above). This doesn't seem super robust though, as if the page title changes, it will break any tests that reference this.

Is this the standard practice? or is there another way to test it.

Thanks, Matt

Upvotes: 0

Views: 1702

Answers (1)

Guilherme Franco
Guilherme Franco

Reputation: 1483

I don't think that the example you gave is the standard way to test that a page is visited. It is the standard way to see if the page's title match what you expect =P

If you want to make an assertion about the path in capybara, a much more reliable way to do it is using current_path. So, you can rewrite your example as follows:

describe "Pages" do
  describe "Home page" do
    it "will visit root_path" do
      visit root_path
      expect(current_path).to eql root_path
    end
  end
end

Notice that this is not a much valuable test though. We all know that capybara is well tested and that it will visit root_path if we tell it to do so. Anyways, if you want to make a sanity check or something, this is the right way to go.

I hope it helps !

Upvotes: 2

Related Questions