Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change subdomain in requests test with Rspec (for API testing)

I have a question which is really specific. I don't want to do a controller test but a requests test. And I don't want to use Capybara because I don't want to test user interaction but only response statuses.

I have the following test under spec/requests/api/garage_spec.rb

require 'spec_helper'

describe "Garages" do

  describe "index" do
    it "should return status 200" do
      get 'http://api.localhost.dev/garages'
      response.status.should be(200)
      response.body.should_not be_empty
    end
  end
end

This works. But as I have to do more tests.. is there any way to avoid to repeat this? http://api.localhost.dev

I tried with setup { host! 'api.localhost.dev' } But it doesn't do anything. A before(:each) block setting @request.host to something, of course crashes because @request is nil before performing any http request.

The routes are set correctly (and in fact they work) in this way

namespace :api, path: '/', constraints: { subdomain: 'api' } do
  resources :garages, only: :index
end
like image 393
jonnyjava.net Avatar asked Jul 09 '14 11:07

jonnyjava.net


People also ask

How do I run an RSpec on a specific file?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.

Is RSpec TDD or BDD?

RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

What is RSpec capybara?

Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language.

Is RSpec used for unit testing?

RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool.


2 Answers

You can create a helper method in the spec_helper.rb, something like:

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

And its usage will be:

require 'spec_helper'

describe "Garages" do

  describe "index" do
    it "should return status 200" do
      my_get 'garages'
      response.status.should be(200)
      response.body.should_not be_empty
    end
  end
end
like image 76
Uri Agassi Avatar answered Oct 13 '22 01:10

Uri Agassi


Try this:

spec_helper.rb

RSpec.configure do |config|
  config.before(:each, type: :api) do |example|
    host! 'api.example.com'
  end
end

your spec file

require 'spec_helper'

describe "Garages", type: :api do

  describe "index" do
    it "should return status 200" do
      get 'garages'
      response.status.should be(200)
      response.body.should_not be_empty
    end
  end
end
like image 25
Xiaohui Zhang Avatar answered Oct 13 '22 01:10

Xiaohui Zhang