74
votes

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

13 Answers

135
votes

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
25
votes

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
25
votes

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

15
votes

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

14
votes

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.

5
votes

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

2
votes

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.

2
votes

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

2
votes

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

--require spec_helper

to:

--require rails_helper
1
votes

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.

0
votes

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

0
votes

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
0
votes

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