【问题标题】:Visit method not found in my rspec在我的 rspec 中找不到访问方法
【发布时间】:2012-02-10 09:19:00
【问题描述】:

我的 java web 应用程序在 http://localhost:8080/tomcat 上运行

编写我的第一个规范,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?如果我的 url 是 localhost:3030 或 localhost:8080 怎么办?

我的宝石文件:

source 'http://rubygems.org'

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

我的 spec_helper:

require 'capybara/rspec'

【问题讨论】:

    标签: ruby-on-rails rspec jruby


    【解决方案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'
    

    【讨论】:

      【解决方案2】:

      还要确保您的测试位于 /spec/features 目录中。根据rspec-rails and capybara 2.0,RSpec 请求规范中默认不提供 Capybara v2 及更高版本。他们建议“......将任何使用 capybara 的测试从规范/请求移至规范/功能。”

      【讨论】:

        【解决方案3】:

        Capybara::RSpec 现在查看包含Capybara::DSLCapybara::RSpecMatchers 的默认目录从requests 更改为features

        在我将 requests 目录重命名为 features 后,我再次获得了匹配器和 DSL 方法,而无需显式包含它们。

        查看以下commit

        【讨论】:

        • 还将“describe”重命名为“feature”
        • 很好的解释。谢谢。但我没有更改目录名称,而是遵循@egezer 的解决方案。两者都有效。
        • 建议将规范/请求重命名(将测试从)重命名为规范/功能。见:andylindeman.com/2012/11/11/…
        • rspec-rails 文档对每种测试类型的默认位置都有很好的提示:github.com/rspec/rspec-rails
        • 让我补充一下:requestsfeatures 都是允许的目录,但用途不同。虽然重命名你的目录起作用,但你可能希望确保你正在编写的规范类型在正确的位置。也就是说,如果你想使用visit(又名,capybara 的东西),那么将这些规范放在 features 目录中,并将请求样式的规范留在 requests 目录中。
        【解决方案4】:

        关于 rspec 问题 (https://github.com/rspec/rspec-rails/issues/360)

        你应该放

        config.include Capybara::DSL
        

        spec_helper.rb 中,在配置块内。

        【讨论】:

        • 这在 Rails 应用程序 3.2.9 中为我修复了它。我真的很想知道实际的问题是什么。我想知道--no-test-unit 在您创建 Rails 应用程序时是否会对此产生某种影响。
        • 我认为更好和推荐的解决方案是将规范/请求重命名为规范/功能
        • @Artur79 -- 我将spec/integration(在我的情况下)重命名为spec/features,但这并没有帮助。不过,根据上述答案修改 spec_helper.rb 确实如此。
        • 现在需要添加到rails_helper.rb
        • 另加config.include Capybara::RSpecMatchers
        【解决方案5】:

        First check it out

        如果你不成功,

        将此代码添加到您的规范帮助程序的末尾,实际上也超出了 RSpec.configure 块

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

        【讨论】:

          【解决方案6】:

          默认情况下,如果文件位于规范/请求、规范/集成中或示例组具有 :type =&gt; :request,则自动包含水豚 DSL。

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

          【讨论】:

          • 我将 spec/home/home_spec.rb 重命名为 spec/requests/home_spec.rb 并没有改变任何东西。添加我现在得到的包含:rack-test requires a rack application, but none was given
          • 我应该在请求文件夹中放置一个支持文件夹吗?即 /spec/requests/support/env.rb ?
          • 啊,我错过了你没有测试 Rails 应用程序。您至少需要将 capybara 驱动程序设置为 selenium,但除此之外我不确定
          • 它会自动加载 spec/requests/support/env.rb 还是我必须以某种方式执行此操作?还是我应该把它放在 /spec/support/env.rb 中??
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-08
          相关资源
          最近更新 更多