我在使用 Capybara 在 Rails 中运行测试时遇到问题。 每当我运行它时,它都会告诉我我的 spec_helper.rb 文件中有一个“未初始化的常量 Capybara (NameError)”。
我正在学习本教程: http://www.railstutorial.org/book/static_pages#code-capybara_dsl
这是我的 spec_helper.rb
RSpec.configure do |config|
config.include(Capybara::DSL)
end
我正在尝试运行这个测试 static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
如果需要更多信息,请告诉我
--------更新----------------
我发现了问题。我拥有的 Rspec 版本在 spec 文件夹中创建了一个 rails_helper.rb 文件。我必须这样做:
require 'capybara/rspec'
在该文件中,config.include Capybara::DSL 在配置中。
-----新问题------------ 但现在我遇到另一个问题,它说“访问”是未定义的。
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::AboutPage:0x000001033f5d50>
我试过 require 'capybara' 和 require 'capybara/dsl' 但它们仍然给我错误,然后是一些错误。
最佳答案
我有点发现了问题,我正在初始化常量 capybara ,因为在较新版本的 rspec 中,当您首次创建 spec 文件夹时,它们有一个新文件夹“rails_helper”,它同时创建了 rails_helper 和 spec_helper。 spec 文件夹中的第一个模拟 Blob 测试在顶部有这个
require 'rails_helper'
我正在学习一个教程,该教程告诉我需要“spec_helper”,但对于较新版本的 rspec 来说并非如此。
所以我加了
config.include(Capybara::DSL)
在 rails helper 文件夹中,一切正常,除了 css 出现错误,但我相信这是因为测试 headless 运行,所以它们不起作用。不过,在显示所有错误后,规范测试运行良好。如果您还有其他问题,请告诉我。
关于ruby-on-rails - 未初始化的常量 Capybara,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24024269/