问题:实作rspec出现的错误(2)

想在spec/features/home_spec.rb下,做测试rspec spec/features/home_spec.rb这个动作,出现报错

require "rails_helper"
RSpec.describe "Home", :type => feature do
  scenario "welcomes user" do
  visit root_url
    expect(page).to have_text "Welcome"
  end
  scenario "has navbar element" do
    visit root_url
    expect(page).to have_css "nav.navbar"
  end
end
   Failure/Error: visit root_url
   NameError:
       undefined local variable or method `root_url' for #<RSpec::ExampleGroups::Home:0x007fccd62750b0>

查找原因应该是rspec内建metadata导致的,采用了错误的路径所以找不到root_url 错误:RSpec.describe "Home", :type => feature do 修改:RSpec.describe "Home" do it will be work 测试登入的方法,先要安装gem(详见rspec101)

require "rails_helper"
  RSpec.feature "User authentication" do 
    scenario "existing user signs in" do 
      create(:user, email:"user@example.com", password: "password" )
      visit "/users/sign_in"
      within(".new_user") do 
        fill_in "Email", with: "user@example.com"
        fill_in "Password", with: "password"
      end
      click_button "Log in"
      expect(page).to have_text "user@example.com"
    end
  end