Ruby-on-rails 在我的 rspec 中找不到访问方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8862967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:37:57  来源:igfitidea点击:

Visit method not found in my rspec

ruby-on-railsrspecjruby

提问by Blankman

My java web application is running on tomcat at http://localhost:8080/

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

Writing my first spec, home_spec:

编写我的第一个规范 home_spec:

require 'spec_helper'


describe "home" do

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

       page.should have_content("hello world")
    end

end

And running:

并运行:

rspec

I get:

我得到:

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

Shouldn't this work because I have included capybara in the spec_helper?

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

How will it know to visit the correct url? what if my url is localhost:3030 or localhost:8080?

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

My gemfile:

我的宝石文件:

source 'http://rubygems.org'

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

My spec_helper:

我的规范助手:

require 'capybara/rspec'

回答by Erdem Gezer

Regarding to rspec issues (https://github.com/rspec/rspec-rails/issues/360)

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

you should put

你应该把

config.include Capybara::DSL

in spec_helper.rb, inside the config block.

spec_helper.rb 中,在配置块内。

回答by steve.clarke

The default directory that Capybara::RSpecnow looks at to include the Capybara::DSLand Capybara::RSpecMatchersis changed from requeststo features.

Capybara::RSpec现在查看以包含Capybara::DSL和的默认目录Capybara::RSpecMatchers从 更改requestsfeatures

After I renamed my requestsdirectory to featuresI got the matcher and DSL methods available again without having to explicitly include them.

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

See the following commit

请参阅以下提交

回答by shadowbrush

Also make sure your tests are in the /spec/featuresdirectory. According to rspec-rails and capybara 2.0, Capybara v2 and higher will not be available by default in RSpec request specs. They suggest to "...move any tests that use capybara from spec/requests to spec/features."

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

回答by Frederick Cheung

By default the capybara DSL is included automatically if the file is in spec/requests, spec/integration or if the example group has :type => :request.

默认情况下,如果文件在 spec/requests、spec/integration 或示例组中有:type => :request.

Because your file is in spec/home the capybara helpers aren't being included. You can either conform to one of the patterns above or adding include Capybara::DSLshould also do the trick (you might also need to replicate some of the before(:each)stuff that would be setup.)

因为您的文件在 spec/home 中,所以不包括水豚助手。您可以符合上述模式之一,include Capybara::DSL也可以添加也可以解决问题(您可能还需要复制一些before(:each)将要设置的内容。)

回答by ndrx42

First check it out

首先检查一下

If you are not success,

如果没有成功,

Add this code your end of the your spec helper actually out of the RSpec.configure block as well

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

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

回答by Anatoliivna

1) Add to ‘rails_helper' config:

1) 添加到“rails_helper”配置:

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

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

2) Add to 'spec_helper':

2) 添加到“spec_helper”:

require 'rails_helper'