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
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.
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.
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.
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.
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
Try this:
RSpec.configure do |config|
config.before(:each, type: :api) do |example|
host! 'api.example.com'
end
end
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With