Skip to content

Instantly share code, notes, and snippets.

@jorbs

jorbs/Gemfile

Last active Jan 8, 2021
Embed
What would you like to do?
Hanami request spec
# spec/api/requests/companies_spec.rb
RSpec.describe "API companies" do
include Rack::Test::Methods
# app is required by Rack::Test
let(:app) { Hanami.app }
it "is successful" do
get "/api/companies?tag=1"
expect(last_response.status).to be(200)
end
end
# config/environment.rb
Hanami.configure do
middleware.use Hanami::Middleware::BodyParser, :json
mount Api::Application, at: '/api'
mount Web::Application, at: '/'
# ...
end
source 'https://rubygems.org'
ruby "2.6.6"
gem 'rake'
gem 'hanami', '~> 1.3'
gem 'hanami-model', '~> 1.3'
gem 'pg', '~> 1.2'
gem 'puma', '~> 5.0'
gem 'sidekiq', '~> 6.1'
gem 'httparty', '~> 0.18'
gem 'sentry-raven', '~> 3.1'
gem 'rack-cors', '~> 1.1'
group :development do
# Code reloading
# See: https://guides.hanamirb.org/projects/code-reloading
gem 'shotgun', platforms: :ruby
gem 'hanami-webconsole'
gem "binding_of_caller"
gem "rdoc"
end
group :test, :development do
gem 'dotenv', '~> 2.4'
gem 'jazz_fingers' # for pretty consoles
end
group :test do
gem 'rspec'
gem 'capybara'
end
group :production do
end
# controllers/company/index.rb
module Api
module Controllers
module Company
class Index
include Api::Action
accept :json
def call(params)
tag = params[:tag]
if tag
tag = tag.split("_").join(" ")
@response = GetCompanies.new.call(tag).companies
else
@response = {data: [], error: "Please supply valid tag"}
end
status_code = if @response[:error]
400
else
200
end
status status_code, JSON.generate(@response)
end
end
end
end
end
F
Failures:
1) API companies is successful
Failure/Error: expect(last_response.status).to be(200)
expected #<Integer:401> => 200
got #<Integer:803> => 401
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
# ./spec/api/requests/companies_spec.rb:14:in `block (2 levels) in <top (required)>'
Finished in 0.03373 seconds (files took 1.67 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/api/requests/companies_spec.rb:10 # API companies is successful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment