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

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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

  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=>#, :unless=>#} F  Failures:    1) ToolsController renders buttons_widgets partial      Failure/Error: get :buttons_widgets      NoMethodError:        undefined method `get' for #<:core::examplegroup::nested_1:0x00000106db51f8> # ./tools_model_spec.rb:7:in `block (2 levels) in ' 

回答1:

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

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

This was changed in 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.configure do |config|   config.infer_spec_type_from_file_location! end 

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

In the rspec-rails README:

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

An example of setting the controller context metadata for RSpec:

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


回答2:

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

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


回答3:

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.configure do |config|   config.infer_spec_type_from_file_location! end 

Rspec upgrade



回答4:

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



回答5:

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



回答6:

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



回答7:

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



回答8:

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

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.



回答9:

I had this issue when I added

gem 'rspec' 

to my Gemfile in the rails project. It should be

gem 'rspec' gem 'rspec-rails' 

(or just rspec-rails). After

bundle install 

re-create the spec directory with

rspec --init 

and put your xxx_spec.rb file in the appropriate directory (won't work if it is in the spec directory). Beginners error but maybe this helps somebody ;) Here's the link that helped me:

https://www.relishapp.com/rspec/rspec-rails/docs/gettingstarted



回答10:

this can happen under the following conditions:

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

  2. your spec is not in the controllers folder or you not have set 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. 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_helper now includes spec_helper (to generate both see the Rspec installation steps)

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!