ruby-on-railsrubyruby-on-rails-4rspecruby-on-rails-5

Getting undefined method `get' for #<RSpec::ExampleGroups in my rspec


Rails 4.2.5, rspec-rails 3.0.2

I want to test my API. So I created requests directory inside /spec. There is a file called projects_spec.rb

Here is the code:

describe 'Projects API' do
  describe 'GET /projects' do
    it 'should return 401 HTTP code' do
      get '/api/v1/projects'
      expect(response.status).to eq(401)
    end
  end
end

And when I run this test I'm getting

NoMethodError: undefined method `get' for #RSpec::ExampleGroups::ProjectsAPI::GETProjects:0x007fee73ad9b48>

What's wrong?

# rails_helper.rb
config.infer_spec_type_from_file_location!

Solution

  • describe 'Projects API', type: :request do
      # ...
    end
    

    Also, make sure you've included require 'rails_helper' in your projects_spec.rb.