【问题标题】:Undefined method `within' using capybara and rspec使用 capybara 和 rspec 的未定义方法`within'
【发布时间】:2013-10-16 11:05:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: rspec capybara


    【解决方案1】:

    在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多