【问题标题】:uninitialized constant Capybara after using Capybara with Rspec将 Capybara 与 Rspec 一起使用后未初始化的常量 Capybara
【发布时间】:2014-07-07 19:31:00
【问题描述】:

我正在通过在线 Ruby on Rails 教程书学习 ruby​​ on rails,我遇到了这个练习 http://www.railstutorial.org/book/static_pages#code-capybara_dsl

我按照添加说明进行操作

RSpec.configure do |config|
  config.include Capybara::DSL
end

到我的 spec_helper.rb,但现在我收到了错误

uninitialized constant Capybara <NameError>

这是我当前的 spec_helper.rb

require "spec_helper"

RSpec.configure do |config|
  config.include Capybara::DSL
end

describe "Static pages" do 
  describe "Home page" do 
    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end

describe "Help page" do

    it "should have the content 'Help'" do
      visit '/static_pages/help'
      expect(page).to have_content('Help')
    end
end

 describe "About page" do

     it "should have the content 'About Us'" do
      visit '/static_pages/about'
      expect(page).to have_content('About Us')
     end
  end
end

我做错了什么?我是否错误地安装了 gem?

编辑:不确定这是否有帮助,但这是我调用 $ bundle exec rspec spec/requests/static_pages_spec.rb 后失败的示例输出

←[31mrspec ./spec/spec_helper.rb:7←[0m ←[36m# Static pages Home page should have
 the content 'Sample App'←[0m
←[31mrspec ./spec/spec_helper.rb:15←[0m ←[36m# Static pages Help page should hav
e the content 'Help'←[0m
←[31mrspec ./spec/spec_helper.rb:23←[0m ←[36m# Static pages About page should ha
ve the content 'About Us'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:5←[0m ←[36m# StaticPages GET /st
atic_pages works! (now write some real specs)←[0m

【问题讨论】:

  • 您能否验证您的 Gemfile 中是否指定了 rspec gem?
  • 我目前有 gem 'rspec-rails', '2.13.1'

标签: ruby-on-rails ruby rspec capybara


【解决方案1】:

愚蠢的我。在本教程中有两个文件我没有仔细查看。一个是 spec/requests 下的 static_pages_spec.rb,另一个是 spec 下的 spec_helper.rb。我把这两个文件弄乱了,把相反的东西放在一起。

在意识到“为什么我已经在 spec_helper.rb 中还需要使用 spec_helper.rb”后,我发现了自己的错误

【讨论】:

    猜你喜欢
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多