使用capybara和rspec的未定义方法“内部” [英] Undefined method `within' using capybara and rspec

查看:67
本文介绍了使用capybara和rspec的未定义方法“内部”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a simple rspec that checks for a Bootstrap navigation bar and the appropriate links inside it. I've included capybara/rspec in spec_helper.rb and have the rspec in the spec/features directory, so I think I have the setup right, but running the spec produces an error stating that the within method is undefined.

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

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

Any ideas for how I can fix this?

推荐答案

在RSpec中,capybara方法仅在 it 块。您只需要将代码重构为类似的内容即可:

In RSpec, the capybara methods are only available inside of an it block. You just need to refactor your code into something like:

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

这篇关于使用capybara和rspec的未定义方法“内部”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆