我的rspec中找不到Visit方法

54
我的Java Web应用正在Tomcat上运行,地址为http://localhost:8080/

编写我的第一个规范,home_spec:

require 'spec_helper'


describe "home" do

    it "should render the home page" do
       visit "/"

       page.should have_content("hello world")
    end

end

并运行:

rspec

我得到:

F

Failures:

  1) home should render the home page
     Failure/Error: visit "/"
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x242870b7>
     # ./spec/home/home_spec.rb:7:in `(root)'

Finished in 0.012 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/home/home_spec.rb:6 # home should render the home page

我已经在spec_helper中包含了capybara,这个应该可以工作,是吗?

它怎么知道访问正确的网址?如果我的url是localhost:3030或localhost:8080会怎样?

我的Gemfile文件:

source 'http://rubygems.org'

gem "activerecord"
gem "rspec"
gem "capybara"
gem "activerecord-jdbcmysql-adapter"

我的spec_helper:

require 'capybara/rspec'
6个回答

135

1
这对我在Rails应用程序3.2.9中修复了它。虽然我真的想知道实际问题是什么。我不知道当你创建一个Rails应用程序时是否使用--no-test-unit会以某种方式影响它。 - Jason Kim
3
我认为更好且推荐的解决方案是将spec/requests重命名为spec/features。 - Artur79
@Artur79 -- 我将spec/integration(在我的情况下)重命名为spec/features,但这并没有帮助。根据上面的答案修改spec_helper.rb是有效的。 - Tass
现在需要将其添加到 rails_helper.rb 中。 - Artem P
还要添加 config.include Capybara::RSpecMatchers - Sergey Makridenkov

23
Capybara::RSpec现在默认搜索的目录已从requests更改为features,以包含Capybara::DSLCapybara::RSpecMatchers
requests目录重命名为features后,无需显式地包含它们即可再次使用匹配器和DSL方法。
请参阅以下提交内容

1
还将“描述”更名为“特性”。 - Marta Kostova
非常好的解释。谢谢你。但是我没有改变目录名称,而是按照@egezer的解决方案进行操作。两种方法都可以。 - PriyankaK
建议将 spec/requests 中的测试重命名(移动)为 spec/features。详情请参见:http://www.andylindeman.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html - Arta
rspec-rails文档对于每种类型的测试的默认位置有很好的提示:https://github.com/rspec/rspec-rails - Jon Kern
让我补充一下: requestsfeatures都是可以接受的目录,但用于不同的目的。虽然重命名你的目录是可行的,但你可能希望确保你正在编写的规范类型放在正确的位置。也就是说,如果你想使用visit(也就是capybara的内容),那么将这些规范放在features目录中,而将请求式的规范放在requests目录中。 - Jon Kern

9

同时确保你的测试位于/spec/features目录中。根据rspec-rails和capybara 2.0,默认情况下,在RSpec请求规范中将无法使用Capybara v2及更高版本。他们建议“将使用capybara的任何测试从spec / requests移动到spec / features。”


6
默认情况下,如果文件位于spec/requests或spec/integration中,或者示例组具有:type => :request,则自动包含capybara DSL。

由于您的文件位于spec/home中,因此未包含capybara助手程序。您可以遵循上述任一模式,或者添加include Capybara :: DSL也可以解决问题(您还需要复制某些将被设置的before(:each)内容)。


我将spec/home/home_spec.rb重命名为spec/requests/home_spec.rb,但没有任何变化。添加include后,现在出现了以下错误:rack-test requires a rack application, but none was given - Blankman
我应该在requests文件夹中放一个支持文件夹吗?例如/spec/requests/support/env.rb? - Blankman
啊,我错过了你没有测试Rails应用程序的事实。你至少需要将Capybara驱动程序设置为Selenium,但除此之外,我不确定。 - Frederick Cheung
它会自动加载spec/requests/support/env.rb吗?还是我需要以某种方式执行此操作?或者我应该将其放在/spec/support/env.rb中? - Blankman

1

先查看一下

如果你没有成功,

将此代码添加到你的 spec helper 的末尾,实际上是在 RSpec.configure 块之外

module ::RSpec::Core
  class ExampleGroup
    include Capybara::DSL
    include Capybara::RSpecMatchers
  end
end

-1

1)将以下配置加入到“rails_helper”中:

config.include Capybara::DSL
config.include Capybara::RSpecMatchers

And comment out the `require 'spec_helper'` line.

2) 添加到 'spec_helper' 文件中:

require 'rails_helper'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接