【问题标题】:Capybara: undefined method 'visit' - Test is already in spec/featuresCapybara:未定义的方法“访问” - 测试已经在规范/功能中
【发布时间】:2014-10-26 08:25:28
【问题描述】:

我刚开始使用 RSpec 和 Capybara,但在我的第一次测试中卡住了。 这是我位于spec/features/pages_spec.rb 的测试代码:

require 'rails_helper'

RSpec.describe "Pages", :type => :request do
    describe "About Page" do
        it "should have the content 'About Us'" do
            visit '/pages/about'
            page.should have_content('About Us')
        end
    end
end

运行测试我得到以下错误:

01:06:59 - INFO - Running: spec

F

Failures:

1) Pages About Page should have the content 'About Us'
 Failure/Error: visit '/pages/about'
 NoMethodError:
   undefined method 'visit' for #<RSpec::ExampleGroups::Pages::AboutPage:0x007f975afe7380>
 # ./spec/features/pages_spec.rb:6:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:44:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:43:in `block (2 levels) in <top (required)>'

Finished in 0.02569 seconds (files took 1.65 seconds to load)

1 example, 1 failure

Failed examples:
rspec ./spec/features/pages_spec.rb:5 # Pages About Page should have the content 'About Us'

我已经搜索了大约一个小时,并且到处都找到了将测试代码从 spec/requests 移动到 spec/features 的解决方案。

我在这里也看到了这个:http://www.rubydoc.info/gems/rspec-rails/file/Capybara.md,大多数解决方案都建议使用它,不建议使用。

# not recommended!
RSpec.configure do |c|
    c.include Capybara::DSL, :file_path => "spec/requests"
end

我不知道如何继续。您的帮助将不胜感激。

【问题讨论】:

  • 这是一个基本问题,但您是否真的在 Gemfile 中包含 Capybara gem 并运行 bundle install
  • 是的,我当然有。

标签: ruby-on-rails rspec capybara


【解决方案1】:

要么将 :type =&gt; :request 从描述块中取出(它会覆盖从目录位置确定的规范类型),要么将其更改为 :type =&gt; :feature

【讨论】:

  • 哦,我忽略了一件小事。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 2013-11-11
  • 1970-01-01
相关资源
最近更新 更多