0

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

4
  • Are you sure that new_apartment_path is set correctly? Being incorrect, the first test would pass and the second would fail as you see.
    – Justin Ko
    Oct 8, 2013 at 11:34
  • I have a :locale prefix to my urls. Maybe that's causing the problem? for instance, in order to user user_path i have to pass it as: user_path (:id => user.id) for it to work. I'm not sure how to do this workaround with the new_apartment_path though...
    – Uri Klar
    Oct 8, 2013 at 14:07
  • Could you run the test it { should have_link('Post your property', href: "/en/apartment/new")} to confirm the problem is the new_apartment_path?
    – Justin Ko
    Oct 8, 2013 at 14:40
  • the problem is indeed with the new_apartment_path. I'll post my solution. Thanks!
    – Uri Klar
    Oct 8, 2013 at 14:51

2 Answers 2

1

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
0

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))}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.