Last active
January 8, 2021 21:43
-
-
Save jorbs/46ee129671a870d09d395dd2d1ec88bf to your computer and use it in GitHub Desktop.
Hanami request spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/environment.rb | |
Hanami.configure do | |
middleware.use Hanami::Middleware::BodyParser, :json | |
mount Api::Application, at: '/api' | |
mount Web::Application, at: '/' | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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