ChatGPT解决这个技术问题 Extra ChatGPT

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

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=>#<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)>'
is there a buttons_widgets action in the controller?
yes, i do. it's complaining about the 'get' method call.
If you just upgraded to rspec 3.x and are now getting this error see @juankuquintana answer down below

C
Community

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

i tried spec/controllers and spec/app/controllers nothing worked... but :type did :D
The same for me: ../controllers/controller_spec.rb did not work. +1 for :type
Mine solution was to restart Spork :)
I put my require 'spec_helper' after other requires in my test and that really messed things up. This solution worked for me after I put my require 'spec_helper' first. +1
I added type: :controller and worked....but is strange! Do I have to add it to all controllers spec? They are already unders controllers folder
k
koopajah

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

When updating to Capybara 2, this may be the case.
This fixed the issue for me. As @tmg said, I have just upgraded Capybara to 2.x
Thanks mate. Issue with latest Capybara here.
j
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.configure do |config|
  config.infer_spec_type_from_file_location!
end

Rspec upgrade


Spent a long time looking for this solution. Thanks!
This is the right answer if you have just upgraded to RSpec 3.
If you use the rails helper from RSpec 3, the spec type will be inferred based on the file location. So there is no need to change this as long as you require 'rails_helper' at the top of every spec file.
As Joost aluded to - make sure you have require 'rails_helper' -- not require 'spec_helper'
this is for rspec-rails
P
Peter Brown

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


s
superstar3000

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.


ugh I facepalmed so hard when I read this and it fixed my issue :/
@catsby 2x facepalmed. le' sigh.
c
curtis jacques

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".


B
Brad Werth

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.


J
Jason FB

this can happen under the following conditions:

your spec does not have :type => :controller [type: :controller in newer Ruby] 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:

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


A
Aleks

If you used rspec to generate the .rspec file, you should change the content from:

--require spec_helper

to:

--require rails_helper

R
RubyFanatic

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.


J
Johannes Thoma

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


F
Foram

For Access request get,post,patch and delete, You can use both request and controller in :type

I prefer :request type for API Rspec and simple :controller for controllers Rspec

Here For Request,

RSpec.describe ToolsController, type: 'request' do

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

end

M
Mohammad Bon

In Gemfile:

In ":Test" block, add

gem 'rspec'

gem 'rspec-rails'

In your Test file:

On top of the test file, you should also require 'rails-helper' library

**In the Test file, when you describe the controller, your should mention that the type of the class is "Controller" like this:

describe MyController, type: :controller do

note: Don't forget to use 'Bundle install' after changing the GemFile


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now