In my routes file, I have this random route without a controller
match "/ping" => lambda{ |env| [200, {'Content-Type' => 'text/plain'}, ['ACK']] }
Using Test::Unit / MiniTest, how would I go about testing, that yes the route /ping return 'ACK'
When you create a route without a controller, you cannot use ActionController::TestCase to test it. Instead, you should use ActionDispatch::IntegrationTest. Create a file at test/integration/ping_test.rb that includes the following:
require "test_helper"
class PingTest < ActionDispatch::IntegrationTest
def test_ping
get "/ping"
assert_response :success
assert_equal "ACK", response.body
end
end
Definitely using integration tests. See details here: http://guides.rubyonrails.org/testing.html#integration-testing
Related
I have a controller spec looking like this:
# config_controller_spec.rb
require "spec_helper"
describe Api::V4::ConfigController, type: :controller do
let(:parsed_response) { response.body.to_json }
describe 'GET app_config' do
it "renders successfully" do
get :app_config
expect(response).to be_success
expect(parsed_response).to eq("{key: val}")
end
end
end
When I run it however, I get:
ActionController::UrlGenerationError:
No route matches {:action=>"app_config", :controller=>"api/v4/config"}
I don't under stand why. I googled around and figured that if I add: use_route: :config to the get call like so: get :app_config, use_route: :config, then it works for some reason, though I don't understand why? But when appending that, I get the following deprecation error:
DEPRECATION WARNING: Passing the `use_route` option in functional tests are deprecated. Support for this option in the `process` method (and the related `get`, `head`, `post`, `patch`, `put` and `delete` helpers) will be removed in the next version without replacement.
Functional tests are essentially unit tests for controllers and they should not require knowledge to how the application's routes are configured. Instead, you should explicitly pass the appropiate params to the `process` method.
Previously the engines guide also contained an incorrect example that recommended using this option to test an engine's controllers within the dummy application.
That recommendation was incorrect and has since been corrected.
Instead, you should override the `#routes` variable in the test case with `Foo::Engine.routes`. See the updated engines guide for details.
Here is my controller:
# config_controller.rb
class Api::V4::ConfigController < Api::V4::BaseController
def app_config
render json: Api::V6::Config.app_config, root: false
end
end
And routes:
# routes.rb
MyApp::Application.routes.draw do
constraints subdomain: /\Awww\b/ do
namespace :api, defaults: {format: 'json'} do
get 'app_config' => 'config#app_config'
end
end
end
Use a request spec instead of a controller spec:
describe "Api V4 Configuration", type: :request do
let(:json) { JSON.parse(response.body) }
subject { response }
describe 'GET app_config' do
before { get "/api/v4/app_config" }
it { should be_successful }
it "has the correct contents" do
expect(json).to include(foo: "bar")
end
end
end
One of biggest changes with Rails 5 was the depreciation of ActionController::TestCase (which RSpec controller specs wrap) in favor of integration tests. Thus using request specs is a more future proof solution - using less abstraction also means that your specs will cover routing properly as well.
Also you don't seem to be nesting your routes properly:
# routes.rb
MyApp::Application.routes.draw do
constraints subdomain: /\Awww\b/ do
namespace :api, defaults: {format: 'json'} do
namespace :v4 do
get 'app_config' => 'config#app_config'
end
end
end
end
See:
Replacing RSpec controller specs
I'm creating an API in Rails and I use versionist to handle versions. I want to test API controllers, but I'm unable to create a valid request.
My controller:
class Api::V1::ItemsController < Api::V1::BaseController
def index
render json:'anything'
end
end
My spec:
describe Api::V1::ItemsController do
describe "#create" do
it "shows items" do
get :index, format: :json
end
end
end
routes.rb:
scope '/api' do
api_version(:module => "Api::V1", :path => {:value => "v1"}, :default => true) do
resources :items
end
end
The test doesn't check anything. Still, it raises an error:
Failure/Error: get :index, format: :json
ActionController::RoutingError:
No route matches {:format=>:json, :controller=>"api/v1/items", :action=>"index"}
I suppose that there is something wrong with the :controller key in the request, but I don't know how to fix it...
I was able to reproduce this locally. You need to move this to a request spec instead of a controller spec for this to work:
# spec/requests/api/v1/items_controller_spec.rb
describe Api::V1::ItemsController do
describe "#index" do
it "shows items" do
get '/api/v1/items.json'
# assert something
end
end
end
The versionist documentation says you need to do this when using the HTTP header or request parameter versioning strategies (https://github.com/bploetz/versionist#a-note-about-testing-when-using-the-http-header-or-request-parameter-strategies) but that's clearly not the case here. I'll file an issue to get this clarified in the documentation that you need to do this for all versioning strategies.
I've overridden Refinery's session controller and not even modified it yet, as I'm trying to write some specs for it.
The controller lives in app/controllers/refinery/sessions_controller:
module Refinery
class SessionsController < Devise::SessionsController
....
def create
super
rescue ::BCrypt::Errors::InvalidSalt, ::BCrypt::Errors::InvalidHash
flash[:error] = t('password_encryption', :scope => 'refinery.users.forgot')
redirect_to refinery.new_refinery_user_password_path
end
.....
I then am trying to write a spec against this in spec/controllers/refinery/sessions_controller_spec.rb:
require 'spec_helper'
describe Refinery::SessionsController do
it "should post ok" do
post :create
response.should be_success
end
end
However this is giving me an error
No route matches {:action=>"create", :controller=>"refinery/sessions"}
This confuses me as when I run rake routes, I get the following line:
refinery_user_session POST /refinery/users/login(.:format) refinery/sessions#create
Could anyone help?
Rails 3.2.8, refinery 2.0.8
I have written a functional test that changes some of the request object's environment variables to simulate a user has logged in.
require 'test_helper'
class BeesControllerTest < ActionController::TestCase
# See that the index page gets called correctly.
def test_get_index
#request.env['HTTPS'] = "on"
#request.env['SERVER_NAME'] = "sandbox.example.com"
#request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER
get :index
assert_response :success
assert_not_nil(assigns(:bees))
assert_select "title", "Bees and Honey"
end
end
The functional test works fine.
Now I want to do something similar as part of integration testing. Here is what I tried:
require 'test_helper'
class CreateBeeTest < ActionController::IntegrationTest
fixtures :bees
def test_create
#request.env['HTTPS'] = "on"
#request.env['SERVER_NAME'] = "sandbox.example.com"
#request.env['REMOTE_USER'] = "joeuser" # Authn/Authz done via REMOTE_USER
https?
get "/"
assert_response :success
[... more ...]
end
end
I get an error complaining that #request is nil. I suspect this has something to do with the session object, but I am not sure how to get it to work.
You can set HTTPS in integration tests with
https!
And set the host name with:
host! "sandbox.example.com"
Which may be equivalent to what you want to do?
This is described in the Rails guides Rails guides
You can change request variables via parameters to post method.
For your case, method test_create will be:
def test_create
https!
get "/", nil, { 'SERVER_NAME'] => "sandbox.example.com", 'REMOTE_USER'] => "joeuser" }
assert_response :success
[... more ...]
end
Same works for setting post request to raw data:
post root_path, nil, { 'RAW_POST_DATA' => 'some string' }
Given I have a named route:
map.some_route '/some_routes/:id', :controller => 'some', :action => 'other'
How do I use the routing spec file 'spec/routing/some_routing_spec.rb' to test for that named route?
I've tried this after the "describe SomeRouteController" block and it doesn't work, I get 'undefined method "helper":
describe SomeRouteHelper, 'some routes named routes' do
it 'should recognize some_route' do
helper.some_route_path(23).should == '/some_routes/23'
end
end
If this is in a controller spec, you can call the routing method directly, no helper needed.
describe SomeController do
it 'should recognize ma routes!' do
thing_path(23).should == '/things/23'
end
end
In RSpec-Rails 2.7+ you can create a spec/routing directory and put your routing specs in there. See the rspec-rails docs for more info.
there's a nice shoulda matcher for this too:
it { should route(:get, "/users/23").to(:action => "show", :id => 23)
more information on using shoulda matchers with rspec:
https://github.com/thoughtbot/shoulda-matchers
You can do this in your controller specs with the assert_routing method, like so:
describe UsersController do
it "should recognize a specific users#show route" do
assert_routing("/users/23", {:controller => "users", :action => "show", :id => 23})
end
end
More documentation is here.
This is how I write the specs using RSpec2, Shoulda, and Capybara. You would save this example file in #{Rails.root}/spec/routing/thingz_routing_spec.rb or my preference #{Rails.root}/spec/routing/thingz_controller_spec.rb
require "spec_helper"
describe ThingzController do
describe "routing" do
it "routes to #index" do
get("/thingz").should route_to("thingz#index")
end
end
end