From e593d1dacb0e3f2abc4c6c8352c71440a4fda2d6 Mon Sep 17 00:00:00 2001 From: raonircl Date: Fri, 21 Oct 2022 02:31:37 -0300 Subject: [PATCH 1/6] routes/controllers tests with rspec --- Gemfile | 1 + Gemfile.lock | 17 +++++++++++++++++ README.md | 25 +++++++++---------------- app/controllers/users_controller.rb | 3 +++ config/routes.rb | 2 +- spec/features/first_test_spec.rb | 15 +++++++++++++++ 6 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 spec/features/first_test_spec.rb diff --git a/Gemfile b/Gemfile index 59e8961..9cb6080 100644 --- a/Gemfile +++ b/Gemfile @@ -32,6 +32,7 @@ group :development, :test do gem 'rspec-rails' gem 'pry-rails' gem 'factory_bot_rails' + gem 'capybara' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 362eb32..e5e38d3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,10 +56,21 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) + addressable (2.8.1) + public_suffix (>= 2.0.2, < 6.0) bootsnap (1.13.0) msgpack (~> 1.2) builder (3.2.4) byebug (11.1.3) + capybara (3.37.1) + addressable + matrix + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (>= 1.5, < 3.0) + xpath (~> 3.2) coderay (1.1.3) concurrent-ruby (1.1.10) crass (1.0.6) @@ -84,6 +95,7 @@ GEM mail (2.7.1) mini_mime (>= 0.1.1) marcel (1.0.2) + matrix (0.4.2) method_source (1.0.0) mini_mime (1.1.2) mini_portile2 (2.8.0) @@ -99,6 +111,7 @@ GEM method_source (~> 1.0) pry-rails (0.3.9) pry (>= 0.10.4) + public_suffix (5.0.0) puma (4.3.12) nio4r (~> 2.0) racc (1.6.0) @@ -135,6 +148,7 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) + regexp_parser (2.6.0) rspec-core (3.11.0) rspec-support (~> 3.11.0) rspec-expectations (3.11.1) @@ -172,6 +186,8 @@ GEM websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) + xpath (3.2.0) + nokogiri (~> 1.8) zeitwerk (2.6.1) PLATFORMS @@ -180,6 +196,7 @@ PLATFORMS DEPENDENCIES bootsnap (>= 1.4.2) byebug + capybara factory_bot_rails listen (~> 3.2) pg (>= 0.18, < 2.0) diff --git a/README.md b/README.md index 7db80e4..1d1bf5f 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,17 @@ -# README -This README would normally document whatever steps are necessary to get the -application up and running. +# api-postgresql -Things you may want to cover: +O projeto conta com uma api construida em Ruby on Rails para fins de desenvolvimento e aprendizado. -* Ruby version -* System dependencies +The project has an API built in Ruby on Rails for development and learning purposes. +## Tutor -* Configuration +- [Joathan](https://github.com/joathan) -* Database creation +## Referência -* Database initialization + - [RubyGems](https://rubygems.org/) +## 🛠 Habilidades +Ruby on Rails, HTML... -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4d064f2..0114f96 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,6 +10,9 @@ def create end end + def index; + end + private # Use callbacks to share common setup or constraints between actions. def set_user diff --git a/config/routes.rb b/config/routes.rb index 5981895..07814fa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :users, only: [:create] + resources :users, only: [:create, :index] # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/spec/features/first_test_spec.rb b/spec/features/first_test_spec.rb new file mode 100644 index 0000000..20992bc --- /dev/null +++ b/spec/features/first_test_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +RSpec.feature "testing" do + scenario "Checking if everything is working" do + visit 'users#index' + + click_link 'novo produto' + + fill_in 'nome', with: 'produto 1' + fill_in 'descrição', with: 'descrição do produto 1 (um)' + click_button 'criar produto' + + expect(page).to have_content('Produto foi cadastrado') + end +end \ No newline at end of file From 67b878c6c3f60971533a55cc28c96aeccf777e06 Mon Sep 17 00:00:00 2001 From: raonircl Date: Fri, 21 Oct 2022 05:08:52 -0300 Subject: [PATCH 2/6] require cabybara --- spec/spec_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a0d4080..386e952 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,4 @@ +require 'capybara/rspec' # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause From d8facc9d26f84ab7dc218b2702f55fc25c99d206 Mon Sep 17 00:00:00 2001 From: raonircl Date: Fri, 21 Oct 2022 07:53:08 -0300 Subject: [PATCH 3/6] testing routes --- app/controllers/users_controller.rb | 10 ++++++++++ config/routes.rb | 2 +- spec/features/first_test_spec.rb | 15 --------------- spec/models/user_spec.rb | 9 +++++++++ 4 files changed, 20 insertions(+), 16 deletions(-) delete mode 100644 spec/features/first_test_spec.rb create mode 100644 spec/models/user_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0114f96..e28130b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -11,6 +11,16 @@ def create end def index; + @user = User.new + render json: @user + end + + def update + if @user.update(user_params) + render json: @user + else + render json: @user.errors, status: :unprocessable_entity + end end private diff --git a/config/routes.rb b/config/routes.rb index 07814fa..86d76ad 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :users, only: [:create, :index] + resources :users, only: [:create, :index, :update] # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/spec/features/first_test_spec.rb b/spec/features/first_test_spec.rb deleted file mode 100644 index 20992bc..0000000 --- a/spec/features/first_test_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'rails_helper' - -RSpec.feature "testing" do - scenario "Checking if everything is working" do - visit 'users#index' - - click_link 'novo produto' - - fill_in 'nome', with: 'produto 1' - fill_in 'descrição', with: 'descrição do produto 1 (um)' - click_button 'criar produto' - - expect(page).to have_content('Produto foi cadastrado') - end -end \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..ab3b965 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +RSpec.feature "testing" do + scenario "Checking if everything is working" do + visit 'users' + @user = User.update( name: 'Di ferrero', age: 34 ) + + end +end From 05a577e4686ad95754585946ccf55f6ab7b23d5d Mon Sep 17 00:00:00 2001 From: raonircl Date: Mon, 7 Nov 2022 02:04:04 -0300 Subject: [PATCH 4/6] test routes, params --- Gemfile | 1 - Gemfile.lock | 35 ++++++++--------------------- app/controllers/users_controller.rb | 7 +++--- spec/models/user_spec.rb | 9 -------- spec/requests/users_spec.rb | 13 +++++++++++ spec/spec_helper.rb | 1 - 6 files changed, 26 insertions(+), 40 deletions(-) delete mode 100644 spec/models/user_spec.rb create mode 100644 spec/requests/users_spec.rb diff --git a/Gemfile b/Gemfile index 9cb6080..59e8961 100644 --- a/Gemfile +++ b/Gemfile @@ -32,7 +32,6 @@ group :development, :test do gem 'rspec-rails' gem 'pry-rails' gem 'factory_bot_rails' - gem 'capybara' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index e5e38d3..c892c91 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,21 +56,10 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) - addressable (2.8.1) - public_suffix (>= 2.0.2, < 6.0) bootsnap (1.13.0) msgpack (~> 1.2) builder (3.2.4) byebug (11.1.3) - capybara (3.37.1) - addressable - matrix - mini_mime (>= 0.1.3) - nokogiri (~> 1.8) - rack (>= 1.6.0) - rack-test (>= 0.6.3) - regexp_parser (>= 1.5, < 3.0) - xpath (~> 3.2) coderay (1.1.3) concurrent-ruby (1.1.10) crass (1.0.6) @@ -95,14 +84,13 @@ GEM mail (2.7.1) mini_mime (>= 0.1.1) marcel (1.0.2) - matrix (0.4.2) method_source (1.0.0) mini_mime (1.1.2) mini_portile2 (2.8.0) minitest (5.16.3) msgpack (1.6.0) nio4r (2.5.8) - nokogiri (1.13.8) + nokogiri (1.13.9) mini_portile2 (~> 2.8.0) racc (~> 1.4) pg (1.4.4) @@ -111,7 +99,6 @@ GEM method_source (~> 1.0) pry-rails (0.3.9) pry (>= 0.10.4) - public_suffix (5.0.0) puma (4.3.12) nio4r (~> 2.0) racc (1.6.0) @@ -148,15 +135,14 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - regexp_parser (2.6.0) - rspec-core (3.11.0) - rspec-support (~> 3.11.0) - rspec-expectations (3.11.1) + rspec-core (3.12.0) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.11.0) - rspec-mocks (3.11.1) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.11.0) + rspec-support (~> 3.12.0) rspec-rails (5.1.2) actionpack (>= 5.2) activesupport (>= 5.2) @@ -165,7 +151,7 @@ GEM rspec-expectations (~> 3.10) rspec-mocks (~> 3.10) rspec-support (~> 3.10) - rspec-support (3.11.1) + rspec-support (3.12.0) shoulda-matchers (5.2.0) activesupport (>= 5.2.0) spring (2.1.1) @@ -186,9 +172,7 @@ GEM websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - xpath (3.2.0) - nokogiri (~> 1.8) - zeitwerk (2.6.1) + zeitwerk (2.6.5) PLATFORMS ruby @@ -196,7 +180,6 @@ PLATFORMS DEPENDENCIES bootsnap (>= 1.4.2) byebug - capybara factory_bot_rails listen (~> 3.2) pg (>= 0.18, < 2.0) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e28130b..90d06de 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,5 @@ class UsersController < ApplicationController + before_action :set_user, only: [:update] def create @user = User.new(user_params) @@ -11,13 +12,13 @@ def create end def index; - @user = User.new - render json: @user + @users = User.all + render json: @users end def update if @user.update(user_params) - render json: @user + render json: @user, status: 200 else render json: @user.errors, status: :unprocessable_entity end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb deleted file mode 100644 index ab3b965..0000000 --- a/spec/models/user_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'rails_helper' - -RSpec.feature "testing" do - scenario "Checking if everything is working" do - visit 'users' - @user = User.update( name: 'Di ferrero', age: 34 ) - - end -end diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb new file mode 100644 index 0000000..842ce16 --- /dev/null +++ b/spec/requests/users_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +RSpec.describe "Users", type: :request do + describe "PUT /users/update" do + let!(:user) { User.create(name: 'John', age: 28) } + scenario 'valid user attributes' do + put "/users/#{user.id}", params: { + user: { name: 'John', age: 28 } + } + expect(response.status).to eq(200) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 386e952..a0d4080 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,3 @@ -require 'capybara/rspec' # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause From 185ed2b91bf3dac37ba5c6d1133d619cddcf2dae Mon Sep 17 00:00:00 2001 From: raonircl Date: Thu, 10 Nov 2022 21:45:25 -0300 Subject: [PATCH 5/6] remove index router --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 86d76ad..b224026 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :users, only: [:create, :index, :update] + resources :users, only: [:create, :update] # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end From 182a7bc6ade625a749dc4268ed1e5de43b54ccb5 Mon Sep 17 00:00:00 2001 From: raonircl Date: Thu, 10 Nov 2022 21:55:01 -0300 Subject: [PATCH 6/6] remove method index --- app/controllers/users_controller.rb | 5 ----- config/routes.rb | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 90d06de..b609db3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -11,11 +11,6 @@ def create end end - def index; - @users = User.all - render json: @users - end - def update if @user.update(user_params) render json: @user, status: 200 diff --git a/config/routes.rb b/config/routes.rb index b224026..3da4fc9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :users, only: [:create, :update] + resources :users, only: [ :create, :update ] # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end