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

Ruby on-RailsRspec

Ruby on-Rails Problem Overview


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)>'

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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.

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

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.

Solution 8 - Ruby on-Rails

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

Solution 9 - Ruby on-Rails

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

--require spec_helper

to:

--require rails_helper

Solution 10 - Ruby on-Rails

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.

Solution 11 - Ruby on-Rails

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

Solution 12 - Ruby on-Rails

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

Solution 13 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question99milesView Question on Stackoverflow
Solution 1 - Ruby on-RailsRob DavisView Answer on Stackoverflow
Solution 2 - Ruby on-RailsHamzaView Answer on Stackoverflow
Solution 3 - Ruby on-RailsjuankuquintanaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsPeter BrownView Answer on Stackoverflow
Solution 5 - Ruby on-Railssuperstar3000View Answer on Stackoverflow
Solution 6 - Ruby on-Railscurtis jacquesView Answer on Stackoverflow
Solution 7 - Ruby on-RailsBrad WerthView Answer on Stackoverflow
Solution 8 - Ruby on-RailsJason FBView Answer on Stackoverflow
Solution 9 - Ruby on-RailsAleksView Answer on Stackoverflow
Solution 10 - Ruby on-RailsRubyFanaticView Answer on Stackoverflow
Solution 11 - Ruby on-RailsJohannes ThomaView Answer on Stackoverflow
Solution 12 - Ruby on-RailsForamView Answer on Stackoverflow
Solution 13 - Ruby on-RailsMohammad BonView Answer on Stackoverflow