gpt4 book ai didi

ruby-on-rails - capybara :未定义的方法 'visit'

转载 作者:行者123 更新时间:2023-12-03 07:45:16 24 4
gpt4 key购买 nike

当使用 rspec 和 capybara 运行我的规范时,它找不到 capybara 的访问方法。我还需要执行其他初始化步骤吗?

$bundle exec rspec spec
/home/brian/projects/expense_track/expense_track/spec/requests/homepage_spec.rb:6:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xb6572b8> (NoMethodError)

gem 文件:

group :test, :development do
gem "rspec-rails"
gem "capybara"
end

我的spec_helper.rb顶部:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

require 'rspec/rails'
require 'capybara/rspec'
require 'rspec/autorun'

homepage_spec.rb:

require 'spec_helper'

describe "The home page" do

context "home page exists" do
visit "/"
page.should have_content("elephants")
end
end

最佳答案

我自己也遇到了这个问题。

所以原因是 Capybara 发生了一些未记录的更改。 Capybara 现在假设任何使用它的东西都需要位于 spec/features 文件夹中,并且它将做出正确的假设。 spec/requests 文件夹中留下的任何内容都将不再起作用。尽管有解决方法。

对于上下文 block ,您可以添加参数 :type => :feature 这将解决该问题,或者您可以更改 describe 方法的名称feature 规范的开头,这也应该改变它。

他们在 Google 群组中宣布了这一变化:https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q

Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.

这在 github 问题中得到了进一步讨论:https://github.com/jnicklas/capybara/issues/814

关于ruby-on-rails - capybara :未定义的方法 'visit',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9059854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com