b-m-f
b-m-f

Reputation: 1328

Test layout in rails 4 with Rspec 3

I put a bootstrap navbar in my layout and want to test whether all the links are correct.

 require 'rails_helper'

describe "layouts/application.html.erb" do



  it "should have the right links in navbar" do
    render
    expect(page).to have_link('Home', href: "/home/index")
    expect(page).to have_link('Games', href: "/games/index")
    expect(page).to have_link('Programming', href: "/programming/index")
    expect(page).to have_link('Bananenmannfrau', href: "/pages/about")
  end

end

This just returns error:

layouts/application.html.erb should have the right links in navbar
     Failure/Error: render
     NameError:
       undefined local variable or method `render' for #<RSpec::ExampleGroups::LayoutsApplicationHtmlErb:0x00000005a8f750>
     # ./spec/layouts/application.html.erb_spec.rb:8:in `block (2 levels) in <top (required)>'

Is this even the correct way of testing it and when yes what am I missing?

Upvotes: 0

Views: 278

Answers (1)

Māris Cīlītis
Māris Cīlītis

Reputation: 173

You should use capybara gem for testing front end! Capybara

Then you will be able to use it like:

require 'rails_helper'

describe "layouts/application.html.erb" do

  it "should have the right links in navbar" do
    visit root_path
    within(".nav-bar-selector") do
      expect(page).to have_content('Home')
      expect(page).to have_content('Games')
      expect(page).to have_content('Programming')
      expect(page).to have_content('Bananenmannfrau')
    end
  end
end

Upvotes: 3

Related Questions