Ruby-on-rails #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8> 的未定义方法`get'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6296235/
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 01:21:51  来源:igfitidea点击:

undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>

ruby-on-railsrspec

提问by 99miles

Anyone know how to get around this? On OSX, trying to get RSpec running with Rails 3.0.7. Full details at: https://gist.github.com/1017044

有谁知道如何解决这个问题?在 OSX 上,尝试让 RSpec 与 Rails 3.0.7 一起运行。完整详细信息:https: //gist.github.com/1017044

  it "renders buttons_widgets partial" do
    get :buttons_widgets
    response.should render_template("buttons_widgets")
  end


→ rspec tools_model_spec.rb
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/version.rb:4: warning: already initialized constant STRING
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/metadata.rb:48: warning: already initialized constant RESERVED_KEYS
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/pending.rb:6: warning: already initialized constant DEFAULT_MESSAGE
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/world.rb:6: warning: already initialized constant PROC_HEX_NUMBER
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/world.rb:7: warning: already initialized constant PROJECT_DIR
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/configuration.rb:43: warning: already initialized constant CONDITIONAL_FILTERS
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/configuration.rb:48: warning: already initialized constant DEFAULT_BACKTRACE_PATTERNS
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/runner.rb:13: warning: already initialized constant AT_EXIT_HOOK_BACKTRACE_LINE
/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core.rb:35: warning: already initialized constant SharedContext
Run filtered excluding {:if=>#<Proc:/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:43>, :unless=>#<Proc:/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:44>}
F

Failures:

  1) ToolsController renders buttons_widgets partial
     Failure/Error: get :buttons_widgets
     NoMethodError:
       undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>
# ./tools_model_spec.rb:7:in `block (2 levels) in <top (required)>'

回答by Rob Davis

RSpec doesn't know that your spec is a controller spec, so your examples don't have access to a getmethod.

RSpec 不知道您的规范是控制器规范,因此您的示例无权访问get方法。

RSpec 2.x assumes that everything in the controllers directory is a controller spec.

RSpec 2.x 假定控制器目录中的所有内容都是控制器规范。

This was changed in RSpec 3:

这在 RSpec 3 中有所改变:

File-type inference disabled by default

Previously we automatically inferred spec type from a file location, this was a surprising behaviour for new users and undesirable for some veteran users so from RSpec 3 onwards this behaviour must be explicitly opted into with:

默认情况下禁用文件类型推断

以前我们从文件位置自动推断规范类型,这对新用户来说是一种令人惊讶的行为,对一些老用户来说是不受欢迎的,因此从 RSpec 3 开始,必须明确选择这种行为:

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#file-type-inference-disabled

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#file-type-in​​ference-disabled

In the rspec-rails README:

rspec-rails README 中

Controller specs default to residing in the spec/controllers folder. Tagging any context with the metadata :type => :controllertreats it's examples as controller specs.

控制器规范默认驻留在spec/controllers folder. 使用元数据标记任何上下文:type => :controller会将其示例视为控制器规范。

An example of setting the controller context metadata for RSpec:

为 RSpec 设置控制器上下文元数据的示例:

describe ToolsController, :type => :controller do
    # ...
end

回答by Hamza

If at all you are using 'spec/features', you may need to add the following to your 'spec_helper.rb'

如果您使用的是“spec/features”,则可能需要将以下内容添加到“spec_helper.rb”

config.include RSpec::Rails::RequestExampleGroup, type: :feature

回答by juankuquintana

In Rspec 3.x the spec type is not automatically inferred from a file location, and you must manually set it, add this to the spec_helper.rb

在 Rspec 3.x 中,spec 类型不会从文件位置自动推断,您必须手动设置它,将其添加到 spec_helper.rb

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

Rspec upgrade

规格升级

回答by superstar3000

For others looking into this. I was trying to track down a undefined method 'get'error. My issue was that I had the getin a describe blockmake sure your getis in an it block.

对于其他人正在研究这个。我试图追踪一个undefined method 'get'错误。我的问题是我有get一个describe block确保你get在一个it block.

回答by Peter Brown

I was able to fix this issue in my app by adding require 'rspec/rails'to my spec_helper file.

通过添加require 'rspec/rails'到我的 spec_helper 文件,我能够在我的应用程序中解决这个问题。

回答by curtis jacques

Solved by replacing the line
describe PagesController dowith RSpec.describe PagesController, :type => :controller do
in the _spec.rbfile in spec folder.
Also to prevent deprecation warning use expect(response).to be_successinstead of response should be_success.
PS: Didn't have to add require "rails_helper".

通过替换线解决
describe PagesController doRSpec.describe PagesController, :type => :controller do
_spec.rb在规范文件夹文件。
还要防止弃用警告使用expect(response).to be_success而不是response should be_success.
PS:没必要加require "rails_helper"

回答by Brad Werth

I got this error when I forgot to add require 'spec_helper'to the top of my spec file or --require spec_helperto my .rspec file.

当我忘记添加require 'spec_helper'到我的规范文件或--require spec_helper我的 .rspec 文件的顶部时,我收到了这个错误。

回答by Aleks

If you used rspecto generate the .rspecfile, you should change the content from:

如果您曾经rspec生成该.rspec文件,则应将内容从:

--require spec_helper

to:

到:

--require rails_helper

回答by Jason FB

this can happen under the following conditions:

这可能在以下条件下发生:

  1. your spec does not have :type => :controller[type: :controllerin newer Ruby]

  2. your spec is not in the controllers folder or you not have set config.infer_spec_type_from_file_location!

  1. 您的规范没有 :type => :controller[type: :controller在较新的 Ruby 中]

  2. 您的规格不在控制器文件夹中或您尚未设置 config.infer_spec_type_from_file_location!

Either #1 or #2 must be setup for your spec. Also, this can happen under this condition as well:

必须为您的规范设置 #1 或 #2。此外,在这种情况下也可能发生这种情况:

  1. you have written a spec using the old-style require 'spec_helper'instead of using the newer require 'rails_helper'. You will note that rails_helpernow includes spec_helper(to generate both see the Rspec installation steps)
  1. 您已经使用旧样式require 'spec_helper'而不是使用较新的require 'rails_helper'. 您会注意到rails_helper现在包括spec_helper(要生成两者,请参阅Rspec 安装步骤

cross referencing GH issue https://github.com/rails/rails-controller-testing/issues/36

交叉引用 GH 问题https://github.com/rails/rails-controller-testing/issues/36

回答by RubyFanatic

An alternative is to specify type: :requestfor your spec. For example:

另一种方法是type: :request为您的规范指定。例如:

RSpec.describe "Widget management", :type => :request do

  it "creates a Widget and redirects to the Widget's page" do
    get "/widgets/new"
    expect(response).to render_template(:new)

    post "/widgets", :widget => {:name => "My Widget"}

    expect(response).to redirect_to(assigns(:widget))
    follow_redirect!

    expect(response).to render_template(:show)
    expect(response.body).to include("Widget was successfully created.")
  end

end

Example taken from here https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec.

示例取自此处https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec