1

我正在尝试创建一个简单的 rspec 来检查 Bootstrap 导航栏和其中的适当链接。我已经在目录中包含capybara/rspecspec_helper.rb拥有 rspec spec/features,所以我认为我的设置是正确的,但是运行规范会产生一个错误,指出 inside 方法未定义。

require 'spec_helper'

feature "HomePage" do 
  before { visit root_path }

  subject { page }

  describe "the navigation bar" do 
    it { should have_selector('.navbar') }

    within ".navbar" do 
      it { should have_link('Game Contest Server', href: root_path) }
      it { should have_link('Users', href: users_path) }
      it { should have_link('Sign Up', href: signup_path) }
    end
  end
end

关于如何解决这个问题的任何想法?

4

1 回答 1

4

在 RSpec 中,capybara 方法仅在it块内部可用。您只需要将代码重构为:

describe "the navigation bar" do 
  it { should have_selector('.navbar') }

  it "should have links" do
     within ".navbar" do 
       should have_link('Game Contest Server', href: root_path)
       should have_link('Users', href: users_path)
       should have_link('Sign Up', href: signup_path)
     end
  end
end
于 2013-10-11T05:40:11.787 回答