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
Related
QUESTION: What have I done wrong that the route_to method remains undefined?
I'm very new to this but I'm trying to develop some route tests via the rspec gem.
My issue is that I am obtaining the error:
undefined method `route_to' for #<RSpec::ExampleGroups::RouteToHomepage
I have already looked through the API for this query, and I've already done the following:
Install gem 'rspec-rails'
In rails_helper.rb
require 'rspec/rails'
In my routing_spec.rb (where I am writing the routes)
require 'rails_helper'
describe "route to homepage" do
it "routes /home to index" do
expect(:get => "/homes").to route_to(
action: "index"
)
end
end
What exactly do I need to change or add, so the "route_to" method is defined? I've already read around and apparently it's defined in the "rspec-rails" gem, which I have, and already included.
From the documentation:
Routing specs are marked by :type => :routing or if you have set
config.infer_spec_type_from_file_location! by placing them in spec/routing.
You didn't say where the routing_spec.rb is located, but if it's inside the folder spec/routing/ then you could choose to enable the above config option.
Otherwise, or in general, you must do this:
require 'rails_helper'
describe "route to homepage", type: :routing do
it "routes /home to index" do
expect(:get => "/homes").to route_to(
action: "index"
)
end
end
Doing this will include the necessary RSpec helper that defines route_to, among other methods.
The Rspec test looks like this:
RSpec.describe Api::UsersController, type: :controller do
describe 'POST #create' do
subject { post :create, params: create_params }
context '...' do
In the controller, I use:
cookies.permanent[:foo] == 'bar'
However, I'm getting:
NameError:
undefined local variable or method `cookies' for #<Api::UsersController:0x000000137702f0>
Another SO question has an answer that says to add type: :request to describe, but this causes other errors. I'd rather just completely stub out cookies so cookies.permanent[:foo] == 'bar' is always false. How do I do that?
By default controllers in rails-api don't include the middleware that handles cookies. If you need cookies then you need to add that middleware:
config.middleware.use ActionDispatch::Cookies
For the above problem, you need to include
ActionController::Cookies
in your spec files
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"
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.
I am new to Rspec and am trying to test the one route in my application. I have installed Rspec and have included the routing file in spec/routing/routes_spec.rb.
My spec is as follows:
require "spec_helper"
describe "Routes" do
it "routes get index" do
expect(:get => "simulations").to route_to(
:controller => "simulations",
:action => "index"
)
end
end
I get this error:
Routes routes get index
Failure/Error: expect(:get => "simulations").to route_to(
NoMethodError:
undefined method `route_to' for #<RSpec::ExampleGroups::Routes:0x007fc32d2f70b8 #__memoized=nil>
# ./spec/routing/routes_spec.rb:6:in `block (2 levels) in <top (required)>'
Any ideas as to why route_to would be undefined? I have verified that the route actually works.
In Rspec 3 you should require 'rails_helper' rather than require 'spec_helper'.
Based on documentation:
Routing specs are marked by :type => :routing or if you have set config.infer_spec_type_from_file_location! by placing them in spec/routing.
So, unless you set the previous option, you should begin your spec with:
describe "Routes", :type => :routing do
As Fire-Dragon-DoL suggests above, you might like to check that the rspec-rails gem is in place.
When you don't have rspec-rails installed and required, and you use to_route method, you will get the same error when running your specs: NoMethodError: undefined method 'route_to'
Given the same setup, when you use be_routable matcher, then you get another error, in the style of: expected {:get=>"/my_models/} to respond to 'routable?'
To remedy these errors
Add rspec-rails to Gemfile
Run bundle install
Add require 'rspec/rails' to spec_helper (or rails_helper)