访问我的rspec中找不到的方法

Bla*_*man 53 rspec ruby-on-rails jruby

我的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
Run Code Online (Sandbox Code Playgroud)

并运行:

rspec
Run Code Online (Sandbox Code Playgroud)

我明白了:

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
Run Code Online (Sandbox Code Playgroud)

这不应该是因为我在spec_helper中包含了水豚吗?

如何知道访问正确的网址?如果我的网址是localhost:3030或localhost:8080怎么办?

我的gemfile:

source 'http://rubygems.org'

gem "activerecord"
gem "rspec"
gem "capybara"
gem "activerecord-jdbcmysql-adapter"
Run Code Online (Sandbox Code Playgroud)

我的spec_helper:

require 'capybara/rspec'
Run Code Online (Sandbox Code Playgroud)

Erd*_*zer 135

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

你应该把

config.include Capybara::DSL
Run Code Online (Sandbox Code Playgroud)

spec_helper.rb中,在配置块内.

  • 我认为更好和推荐的解决方案是将规格/请求重命名为spec/features (3认同)

小智 23

Capybara::RSpec现在查看的默认目录包含Capybara::DSLCapybara::RSpecMatchers更改requestsfeatures.

在我重命名我的requests目录后,features我再次获得了matcher和DSL方法,而无需明确地包含它们.

请参阅以下提交


小智 9

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


Fre*_*ung 6

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

因为您的文件在spec/home中,所以不包括capybara助手.你可以遵循上面的模式之一,或者添加include Capybara::DSL也应该做的伎俩(你可能还需要复制一些before(:each)将要设置的东西.)