undefined method `get' for #

前端 未结 12 827
野性不改
野性不改 2020-11-30 00:57

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 but         


        
相关标签:
12条回答
  • 2020-11-30 01:34

    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 讨论(0)
  • 2020-11-30 01:35

    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.

    0 讨论(0)
  • 2020-11-30 01:43

    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

    0 讨论(0)
  • 2020-11-30 01:46

    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
    
    0 讨论(0)
  • 2020-11-30 01:46

    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

    0 讨论(0)
  • 2020-11-30 01:48

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

    0 讨论(0)
提交回复
热议问题