Include Rack/Test to perform API Testing
Start testing your REST API in RSpec.
In other to get the awesome functionality of writing restful routes like ’/users.json’ instead of the usual :update for example:
# spec/controller/whatever.rb
describe ... do
patch '/users.json' {key: 'value}
expect(response).to be_success
end
You’ll need to use Rack/Test
First, in your Gemfile:
# Gemfile
...
gem "rack-test", require: "rack/test"
Then, in your spec/spec_helper.rb, Add the following in the outer block:
# spec/spec_helper.rb
module ApiHelper
require 'rack/test'
include Rack::Test::Methods
def app
Rails.application
end
end
# and inside the RSpec config block
RSpec.configure do |config|
config.include ApiHelper
end
And you’re done.
Now you can do this in your tests!
get '/users'
Note
In Rack::Test, the response object is not response (like in ActionController::TestCase::Behavior) but last_response.
References: