Rspec Route Testing and Hosts

Authenticated Route not working for Rspec test

I take it you are using rspec-rails in your rails app.
Rspec-rails sets up a lot of convenience methods for you, but it also introduces some black-magic, which can lead to some unexpected results - like this.

As you can see here it is explained in the comments for controller specs:

# Supports a simple DSL for specifying behavior of ApplicationController.
# Creates an anonymous subclass of ApplicationController and evals the
# `body` in that context. Also sets up implicit routes for this
# controller, that are separate from those defined in "config/routes.rb".

I guess the logic here is, controller features are different from routing and should be tested separately (and indeed rspec-rails offers a test group for routing), so we do not need the routes for controller specs, meaning you should be able to test your controller without setting up the routes.

In my oppinion, testing the redirect for unauthenticated users is more of an integration test, since it requires multiple parts of your application to work together and as such should not be tested in the controller context, but rather as a feature in some blackbox test.

You can write integration tests by placing them in one of these directories spec/requests, spec/api, and spec/integration or by explicitely declaring their type with

RSpec.describe "Something", type: :request do

or place it in spec/features or declare the type as

RSpec.describe "Something", type: :feature do

depending on which level you want to test the redirect (meaning: only test the request-response cycle, or run it in a simulated browser).
Please refer to the documentation for integration tests on the rspec-rails github page for more information.

Rspec routing test fails for wildcard routes

Rails assigns the part of the path ('foo/bar') that matches the wildcard segment (*path) to a request parameter with the same name as the segment, so the action can see how the client got there.

You just need to add the parameter to the result that your test expects:

describe UserController do
describe 'wildcard route'
it "routes to users#index with path set to the requested path" do
expect(get: '/foo/bar').to route_to(
controller: 'users', action: 'index', path: 'foo/bar')
end
end
end

Rails Path Helpers not selecting application host for requests

As @rmlockerd mentioned above, this seems to go back to an old issue with action dispatch that may have somehow resurfaced with Rails 6

It's caused by this code in ActionDispatch::Integration::Session which hard codes example.com as the DEFAULT_HOST.

https://github.com/rails/rails/blob/d45baa34445379319f5c46f6d13f52e6dd79bc9a/actionpack/lib/action_dispatch/
testing/integration.rb#L84

There is some further info in this rails issue https://github.com/rails/rails/issues/546

Given that, you can monkey patch that class to set your default host and protocol to whatever you want. Probably terrible and I'm sure there's a better way but I don't know what it is.

So in the main scope in my rails_helper.rb I added

module ActionDispatch
module Integration #:nodoc:
class Session
DEFAULT_HOST = "localhost"

def https?
true
end
end
end
end

RSpec controller testing http://test.host/login error on index spec test

You can set it globally in your spec_helper.rb file:

Since your spec is set to type: :controller, you would do it this way:

RSpec.configure do |config|
# other config options

config.before(:each) do
@request.host = "localhost:3000"
end
end

However, as of RSpec 3.5 the preferred method of testing controllers is with a :request spec. So if possible, change your spec to type: :request and then you would do it this way:

RSpec.configure do |config|
# other config options

config.before(:each) do
host! "localhost:3000"
end
end

RSpec routing spec: sequence of fields in hash

The problem is not the order of the hash, the problem is the content.

"article_id"=>"5"

is not the same as

"article_id"=> 5

Us the string version in your route_to parameters and that'll fix the issue.

How to test subdomain constraint with RSpec & Rails 4

You can accomplish this by using the full URL in your tests instead of setting the host in a before block.

Try:

RSpec.describe FrontendAPI::EventsController do
describe 'GET #index' do
let(:url) { 'http://subdomain.example.com' }
let(:bad_url) { 'http://foo.example.com' }

context 'wrong subdomain' do
it 'responds with 404' do
get "#{bad_url}/route"
expect(response).to have_http_status(:not_found)
end
end
end
end

There is a similar question and answer here testing routes with subdomain constraints using rspec

testing routes with subdomain constraints using rspec

I usually do:

let(:url)     { "http://api.domain.com"     }
let(:bad_url) { "http://bad_url.domain.com" }

it "does allow creation of sign ups" do
{:post => "#{url}/sign_ups"}.should route_to(
:controller => "sign_ups",
:action => "create",
)
end

it "should not route" do
{:post => "#{bad_url}/sign_ups"}.should_not route_to(
:controller => "sign_ups",
:action => "create",
)
end


Related Topics



Leave a reply



Submit