Undefined `get 'method for # <RSpec :: Core :: ExampleGroup :: Nested_1: 0x00000106db51f8>
Does anyone know how to get around this? On OSX, trying to get RSpec to work with Rails 3.0.7. Full details: 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)>' RSpec does not know that your spec is a controller specification, so your examples do not have access to the get method.
RSpec 2.x assumes that everything in the controller catalog is a controller specification.
This has been changed in RSpec 3:
File type output is disabled by default
Previously, we automatically determined the type of specification from the file location, this was unexpected behavior for new users and undesirable for some veteran users, therefore with RSpec 3 onwards this behavior should be explicitly allowed 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 rspec-rails README :
Controller specifications by default remain in the
spec/controllers folder. Marking any context with metadata:type => :controllertreats it as controller parameters.
Example of configuring controller context metadata for RSpec:
describe ToolsController, :type => :controller do # ... end If you use spec / features at all, you may need to add the following to your spec_helper.rb
config.include RSpec::Rails::RequestExampleGroup, type: :feature In Rspec 3.x, the specification type is not automatically inferred from the file location, and you must manually set it by adding this to spec_helper.rb
RSpec.configure do |config| config.infer_spec_type_from_file_location! end For others learning this. I tried to track the undefined method 'get' error. My problem was that I had get in describe block , make sure your get is in it block .
I was able to fix this problem in my application by adding require 'rspec/rails' to my spec_helper file.
Solved by replacing the describe PagesController do with RSpec.describe PagesController, :type => :controller do
in the _spec.rb file in the specifications folder.
Also, to prevent the obsolescence warning, use expect(response).to be_success instead of response should be_success .
PS: There was no need to add require "rails_helper" .
I got this error when I forgot to add require 'spec_helper' to the top of my spec file or --require spec_helper to the .rspec file.
An alternative is to specify type: :request for your specification. For example:
RSpec.describe "Widget management", :type => :request do it "creates a Widget and redirects to the Widget 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 An example taken here https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec .
I had this problem when I added
gem 'rspec' for my gemfile in rails project. It should be
gem 'rspec' gem 'rspec-rails' (or just rspec-rails). After
bundle install re-create the specification catalog with
rspec --init and put the xxx_spec.rb file in the appropriate directory (will not work if it is in the specifications directory). Beginners mistake, but maybe this helps someone;) Here is the link that helped me:
https://www.relishapp.com/rspec/rspec-rails/docs/gettingstarted
this can occur under the following conditions:
your specification does not have
:type => :controller[type: :controllerin new Ruby]your qualifier is not in the controllers folder or you did not set
config.infer_spec_type_from_file_location!
For your specification you need to configure either # 1 or # 2. In addition, this can happen under this condition:
- You wrote a specification using the old style
require 'spec_helper'instead of using the newerrequire 'rails_helper'. You will notice thatrails_helpernow includesspec_helper(for generating both, see the steps for installing Rspec )
cross reference to GH issue https://github.com/rails/rails-controller-testing/issues/36