Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError: undefined method `get' when running rspec and making get/visit calls

I am completely stuck here, and hoping someone can point me in the right direction. I am trying to use rspec to test my webroutes. I followed the example here:

https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

with my spec file being named:api_tests_spec.rb in the spec/requests folder.

The file is as follows:

require 'spec_helper'

describe "APITests" do
  describe "GET /regions"  do
    it "should return a valid response" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get "/regions.json"
      response.status.should be(200)
      #print response.body
    end
  end
end

Unfortunately the failure message I am getting is as follows:

1) APITests GET /regions should return a valid response Failure/Error: get "/regions.json" NoMethodError: undefined method `get' for # # ./api_tests_spec.rb:7

Does anyone have any insight into why the method can't be found? Everything I have read seems to suggest that something isn't getting included properly, but the solution always seems to be to move the file into the requests folder (where mine already is). Thanks in advance!

The spec_helper.rb file looks like this:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false
end
like image 538
akhalsa Avatar asked Aug 08 '12 18:08

akhalsa


1 Answers

Ok, after the first answer I posted seemed to work for a bit, it stopped (Still not sure why?!)

However, making this change to the api_tests_spec.rb file fixes it:

require 'spec_helper'

describe "APITests", :type => :request do
  describe "GET /regions"  do
    it "should return a valid response" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get "/regions.json"
      response.status.should be(200)
      #print response.body
    end
  end
end

Does anyone have any idea why the :type => :request is required? I thought just by placing this in the requests folder under it should assume they were requests?

like image 102
akhalsa Avatar answered Sep 28 '22 03:09

akhalsa