水豚:未定义的方法"访问" - 测试已经在规范/功能中



我刚开始使用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

我不知道该怎么办。我们将感谢您的帮助。

从描述块中取出:type => :request(覆盖由目录位置确定的规范类型)或将其更改为:type => :feature

最新更新