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

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

Rails: How to test subdomains with RSpec

In test unit i used something like this to set the request.host to come from a subdomain:

def get_sub(sub = "one")
@request.host = "#{sub}.local.me"
end

I personally would put that into the spec_helper.rb file and reference when you need.

For you, in those tests, you're setting sub to equal "kong" probably like

before :each do
get_sub("kong")
end

This joker also has an answer too, which i found after through google

rspec POST api testing on subdomain

Try setting the routes up like this instead:

Rails.application.routes.draw do
constraints subdomain: 'api' do
scope module: 'api', as: 'api' do
namespace 'v1' do
resources :things
end
end
end
end

In order to set up a versioned api you should put each controller in a "version module". So your Api::ThingsController should be:

# controllers/api/v1/things_controller.rb
class Api::V1::ThingsController
# ...

# POST /api/v1/things
def create
end
end

And then you can test it with:

RSpec.describe Api::V1::ThingsController, type: :controller do
describe "POST #create" do
it "returns http success" do
post :create, { thing: { foo: bar }, format: :json}
expect(response).to have_http_status :created
expect(response.headers['location']).to eq thing_path(Thing.last)
end
end
end

But if you really want to test the routing layer as well use a request spec instead. Which is like a feature spec but without the overhead of Capybara.

RSpec tests fails with subdomain path

I don't know the really great way to solve this problem, but I did it using pow server with Anvil.
I have installed Anvil and run server with domain orca.dev. After this I reconfigured my RSpec testing routes.

spec_helper.rb

def get_api_path path, *args
get "http://api.orca.dev#{path}", *args
end

regions_spec.rb

before do
get_api_path '/public/v1/regions/origin?title=indm'
end

And now tests are working!

Rails integration tests with subdomains, RSpec and capybara-webkit (for JavaScript)

From your log it looks like your request to "http://subdomain6.lvh.me:31234/users/sign_in" is redirecting to "http://lvh.me:31234/" which would happen if the account didn't exist. I'm guessing you haven't disabled transactional testing which would mean the app can't actually see the records created in your test thread. See - https://github.com/jnicklas/capybara#transactions-and-database-setup and https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example



Related Topics



Leave a reply



Submit