Getting undefined method `get' for #<RSpec::ExampleGroups in my rspec - ruby-on-rails

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!

describe 'Projects API', type: :request do
# ...
end
Also, make sure you've included require 'rails_helper' in your projects_spec.rb.

Related

RoR rspec-rails error NoMethodError: Undefined method ‘get’

everybody!
I am doing RoR requests test and I am getting this error:
Failures:
1) Users GET #index is a success
Failure/Error: before(:example) { get "/users" }
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::Users::GETIndex:0x000055a33f143f60>
Did you mean? gets
gem
# ./spec/requests/users_spec.rb:6:in `block (3 levels) in <top (required)>'
My code is:
RSpec.describe 'Users', type: :request do
describe 'GET #index' do
before(:example) { get "/users" }
it 'is a success' do
expect(response).to have_http_status(:ok)
end
it "renders 'index' template" do
expect(response).to render_template(:index)
end
it "shows the rendered text 'Welcome to the Ruby on Rails Blog'" do
expect(response).to include('Welcome to the Ruby on Rails Blog')
end
end
end
I did not include require ‘rails_helper’ in the spec file, because when I do this I am getting this error
ChildProcess::MissingFFIError:
FFI is a required pre-requisite for Windows or posix_spawn support in the ChildProcess gem. Ensure the `ffi` gem is installed. If you believe this is an error, please file a bug at http://github.com/enkessler/childprocess/issues
ruby version => ruby 3.1.1p18
rails version => Rails 7.0.2.4
RSpec version => RSpec 3.11

Devise Rspec undefined method `env' for nil:NilClass

I've scoured SO and no one else's results seem to work for me:
I have a ControllerHelper method for my Spec based off what was suggested for Devise:
def login_existing_user
#request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
company = FactoryGirl.create(:company)
user.company_id = company.id
sign_in user
end
I am also creating a Company in this method since that's step 2 of the sign up process for a user to be able to get my authenticated homepage.
At this point, I'm just trying to log the user in with my ScansControllerSpec:
RSpec.describe ScansController, type: :controller do
before(:all) do
login_existing_user
#device = build(:device)
end
describe "GET #create" do
it "returns http success" do
get :create, :device_id => #device.id
puts response
# expect(response).to have_http_status(:success)
end
end
....
end
But I'm getting this for every one of my CRUD method RSpecs:
1) ScansController GET #create returns http success
Failure/Error: #request.env["devise.mapping"] = Devise.mappings[:user]
NoMethodError:
undefined method `env' for nil:NilClass
# ./spec/support/controller_helpers.rb:15:in `login_existing_user'
# ./spec/controllers/scans_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
As other posts have suggested, I am including the Devise::TestHelpers in my rails_helper.rb file. I've also included my ControllerHelpers:
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.include Warden::Test::Helpers
Warden.test_mode!
config.infer_spec_type_from_file_location!
config.include ControllerHelpers, type: :controller
config.after do
Warden.test_reset!
end
end
In the end, I need to be able to log a user in to test that protected controller methods work. I'm not sure if this is an association problem, so I'll add that a user has to have a company, and a company has to have a subscription in order to successfully log in.
... but I can't even get that far since this error is holding me back.
Seemed to have gotten past this issue by following: http://willschenk.com/setting-up-testing/

How to resolve "No route matches" functional test errors in Rails 5

I've upgraded to Rails 5. My first hurdle in getting specs to pass is a 'No route matches' error.
Please see my test and test_helper below. Is there anything I need to add to test_helper or test.rb? Anyone know the cause or how to resolve this?
.....
I've been running a single test while trying to simply get a pass:
bin/rails test test/controllers/users_controller_test.rb:31
which is the 'should get new' line in my users_controller_test.rb
require 'test_helper'
describe UsersController do
//class UsersControllerTest < ActionDispatch::IntegrationTest
before do
glenn = users(:glenn)
sign_in(glenn)
end
it 'should get new' do
get new_user_url
value(response).must_be :success?
end
end
this results in the following error.
Error:
UsersController#test_0002_should get new:
ActionController::UrlGenerationError: No route matches {:action=>"http://test.host/users/new", :controller=>"users"}
test/controllers/users_controller_test.rb:32:in `block (2 levels) in <top (required)>'
test_helper.rb
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/rails'
class ActionController::TestCase
include ActiveJob::TestHelper
include Devise::Test::ControllerHelpers
end
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
end
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
include ActionDispatch::TestProcess # fixture_file_upload
end
I had this same issue and was able to get it to work, though I'm not entirely sure about the internals of why it works.
Apparently the type of spec is necessary.
What I had that resulted in similar error as above:
require 'rails_helper'
RSpec.describe TimingsController, type: :controller do
describe "GET new" do
it "renders the new template" do
get new_timing_path
expect(response).to be_successful
end
end
end
What I have now that works:
require 'rails_helper'
RSpec.describe TimingsController, type: :controller do
describe "GET new", type: :request do
it "renders the new template" do
get new_timing_path
expect(response).to be_successful
end
end
end
So it seems like the type: request part is essential.
While troube-shooting this I created a new rails 5 app, installed devise and minitest-rails...and those tests are passing. The new url style syntax is working fine inside the describe block in my controller tests. However, in the app that pertains to this questions...the url syntax is not working inside the describe block and the fix---at least for now, was to replace the describe block with a class that inherits from ActionDispatch::IntegrationTest. I have no idea, yet, why this is.
Does setting host help for your case?
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-host
host! "subdomain.example.com"

RSpec 3 Error undefined method `get' in request test

After migration on RSpec 3 request test fails.
Here is my test
describe 'GET /api/v1/activities' do
let!(:user) { create(:user) }
subject { json['activities'] }
context 'when not authenticated' do
let(:token) { user.authentication_token }
let(:email) { 'unregistered.user#mail.com' }
before :each do
get '/api/v1/activities', {}, {token: token, email: email}
end
it { expect(response).to_not be_success }
end
end
Here is rspec log
Activities GET /api/v1/activities when not authenticated
Failure/Error: get '/api/v1/activities', {}, {token: token, email: email}
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::Activities::GETApiV1Activities::WhenNotAuthenticated:0x0000000becfaf8>
What can it be?
Ok, migration to RSpec3 was the cause of this issue.
I'd resolved it by migrating to RSpec 2.99.0 and I received a hint to add this code to my spec_helper
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
Now it works. Hurray!
I already had infer_spec_type_from_file_location! set, but we still used the capybara feature syntax here, which probably sets the type to feature. Once I changed feature to describe, it worked.
Try to add helper to request specs. In spec_helper.rb:
config.include RSpec::Rails::RequestExampleGroup, type: :request
Also i assume that you have type: :request in you spec definition.

problem with rspec test, undefined method 'post'

I am writing a spec to test the behavior of the mashup_controller when someone sends a query through a URL. I need to simulate the parameters contained in the URL, and i read that the post() method will do that, however when i get an error:
1) MashupController simulates query
Failure/Error: post :create
NoMethodError:
undefined method `post' for
#<RSpec::Core::ExampleGroup::Nested_1:0x980bc50>
# ./mashup_controller_rspec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.20199 seconds 1 example, 1 failure
Failed examples:
rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query
Here is my code:
require 'spec_helper'
require 'mashup_controller.rb'
describe MashupController do
it "simulates query" do
post :create
end
end
Sorry if I'm not making any sense. I am very new to rails and rspec. Any help would be appreciated. Thanks.
If the spec file is not under spec/controllers, methods like get and post will not be automatically made available by rspec-rails.
You either need to tag your spec:
describe MyController, type: :controller do
# ...
end
or include the module:
describe MyController do
include RSpec::Rails::ControllerExampleGroup
# ...
end
See the relevant code in rspec-rails.
Make sure you have gem spec-rails in your Gemfile
Your mashup_controller_rspec.rb should be under spec/controllers
I used gem rspec-rails instead of gem spec-rails.
In Rails 4, you can declare the type of the RSpec tests as :request and the spec file can be in any directory.
example: in spec/routes/users.rb
RSpec.describe 'UserRoutes', type: :request do
...
end
My solution is
describe MyController, type: :controller
...
end

Resources