Last active
November 11, 2020 03:21
-
-
Save serradura/876e6fc7904c50a129ddf96fd3160616 to your computer and use it in GitHub Desktop.
minitest VS rspec
This file contains hidden or 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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'sinatra' , '~> 2.1' , require: 'sinatra/base' | |
gem 'sqlite3' , '~> 1.4', '>= 1.4.2' | |
gem 'activerecord', '~> 6.0', '>= 6.0.3.4', require: 'active_record' | |
gem 'minitest' , '~> 5.14', '>= 5.14.2' | |
gem 'rack-test', '~> 1.1', require: 'rack/test' | |
end | |
require 'active_support/all' | |
ActiveRecord::Base.establish_connection( | |
host: 'localhost', | |
adapter: 'sqlite3', | |
database: ':memory:' | |
) | |
ActiveRecord::Schema.define do | |
create_table :users do |t| | |
t.column :name, :string | |
t.column :email, :string | |
end | |
end | |
class User < ActiveRecord::Base | |
end | |
class WebAPI < Sinatra::Base | |
get '/' do | |
attributes = [:id, :name, :email] | |
User.pluck(*attributes) | |
.map { |values| attributes.zip(values).to_h } | |
.to_json | |
end | |
end | |
require 'minitest/autorun' | |
class WebApiGetRootTest < ActiveSupport::TestCase | |
include Rack::Test::Methods | |
def app | |
WebAPI.new | |
end | |
setup { User.create(name: 'Rodrigo', email: '[email protected]') } | |
teardown { User.delete_all } | |
test 'GET /' do | |
get '/' | |
assert_equal(200, last_response.status) | |
json = JSON.parse(last_response.body) | |
assert_kind_of(Array, json) | |
assert_equal(1, json.size) | |
assert_kind_of(Integer, json.dig(0, 'id')) | |
assert_equal('Rodrigo', json.dig(0, 'name')) | |
assert_equal('[email protected]', json.dig(0, 'email')) | |
end | |
end |
This file contains hidden or 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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'sinatra' , '~> 2.1' , require: 'sinatra/base' | |
gem 'sqlite3' , '~> 1.4', '>= 1.4.2' | |
gem 'activerecord', '~> 6.0', '>= 6.0.3.4', require: 'active_record' | |
gem 'minitest' , '~> 5.14', '>= 5.14.2' | |
gem 'rack-test', '~> 1.1', require: 'rack/test' | |
end | |
require 'json' | |
ActiveRecord::Base.establish_connection( | |
host: 'localhost', | |
adapter: 'sqlite3', | |
database: ':memory:' | |
) | |
ActiveRecord::Schema.define do | |
create_table :users do |t| | |
t.column :name, :string | |
t.column :email, :string | |
end | |
end | |
class User < ActiveRecord::Base | |
end | |
class WebAPI < Sinatra::Base | |
get '/' do | |
attributes = [:id, :name, :email] | |
User.pluck(*attributes) | |
.map { |values| attributes.zip(values).to_h } | |
.to_json | |
end | |
end | |
require 'minitest/autorun' | |
class WebApiGetRootTest < Minitest::Test | |
include Rack::Test::Methods | |
def app | |
WebAPI.new | |
end | |
def setup | |
User.create(name: 'Rodrigo', email: '[email protected]') | |
end | |
def teardown | |
User.delete_all | |
end | |
def test_get | |
get '/' | |
assert_equal(200, last_response.status) | |
json = JSON.parse(last_response.body) | |
assert_kind_of(Array, json) | |
assert_equal(1, json.size) | |
assert_kind_of(Integer, json.dig(0, 'id')) | |
assert_equal('Rodrigo', json.dig(0, 'name')) | |
assert_equal('[email protected]', json.dig(0, 'email')) | |
end | |
end |
This file contains hidden or 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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'sinatra' , '~> 2.1' , require: 'sinatra/base' | |
gem 'sqlite3' , '~> 1.4', '>= 1.4.2' | |
gem 'activerecord', '~> 6.0', '>= 6.0.3.4', require: 'active_record' | |
gem 'rspec' , '~> 3.10' | |
gem 'rack-test', '~> 1.1', require: 'rack/test' | |
end | |
require 'json' | |
ActiveRecord::Base.establish_connection( | |
host: 'localhost', | |
adapter: 'sqlite3', | |
database: ':memory:' | |
) | |
ActiveRecord::Schema.define do | |
create_table :users do |t| | |
t.column :name, :string | |
t.column :email, :string | |
end | |
end | |
class User < ActiveRecord::Base | |
end | |
class WebAPI < Sinatra::Base | |
get '/' do | |
attributes = [:id, :name, :email] | |
User.pluck(*attributes) | |
.map { |values| attributes.zip(values).to_h } | |
.to_json | |
end | |
end | |
require 'rspec/autorun' | |
RSpec.describe WebAPI do | |
include Rack::Test::Methods | |
let(:app) { WebAPI.new } | |
context 'GET /' do | |
before { User.create(name: 'Rodrigo', email: '[email protected]') } | |
after { User.delete_all } | |
it 'returns a json' do | |
get '/' | |
expect(last_response.status).to be == 200 | |
json = JSON.parse(last_response.body) | |
expect(json).to be_a_kind_of(Array) | |
expect(json).to have_attributes(size: 1) | |
expect(json.first).to include( | |
'id' => a_kind_of(Integer), | |
'name' => 'Rodrigo', | |
'email' => '[email protected]' | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment