简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM