Uri Klar
Uri Klar

Reputation: 3988

Rspec have_link failing where have_selector passes

I have these two tests under requests/pages_spec.rb

pages_spec.rb

require 'spec_helper'

describe "Pages" do
    subject { page } 
    before { visit root_path }

    describe "Home page" do
        it { should have_selector('a', text: 'Post your property')}
        it { should have_link('Post your property', href: new_apartment_path)}
    end
end

The first test passes and the second one fails saying :

expected link "Post your property" to return something

This is the html:

<a href="/en/apartment/new">Post your property</a>

Any idea why the test is failing? Thanks! Uri

Upvotes: 0

Views: 467

Answers (2)

Uri Klar
Uri Klar

Reputation: 3988

Thanks for the help! This is what ended up solving it:

The problem was with the named route new_apartment_path not being recognized because of i18n.

What solved it was adding the line:

let(:locale) { 'en' } 

and changing the test to:

it { should have_link('Post your property', href: new_apartment_path(locale))}

Upvotes: 0

Iralution
Iralution

Reputation: 480

Maybe the context of your should have_link is wrong. You could test it by using save_and_open_page of the capybara gem like the following code and check the html at that testing point.

require 'spec_helper'

describe "Pages" do
    subject { page } 
    before { visit root_path }

    describe "Home page" do
        it { should have_selector('a', text: 'Post your property')}
        save_and_open_page
        it { should have_link('Post your property', href: new_apartment_path)}
    end
end

Upvotes: 1

Related Questions