From 5607707d6b7f53eda16c995759efacef53add0cd Mon Sep 17 00:00:00 2001 From: leogiraldimg Date: Mon, 2 Dec 2019 22:04:33 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Implementando=20testes=20funcionais=20+=20t?= =?UTF-8?q?estes=20unit=C3=A1rios=20-=20favorite=5Fcolleges?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../favorite_colleges_controller_spec.rb | 29 +++++++++++++++++++ src/app/spec/models/favorite_college_spec.rb | 20 ++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/app/spec/controllers/favorite_colleges_controller_spec.rb diff --git a/src/app/spec/controllers/favorite_colleges_controller_spec.rb b/src/app/spec/controllers/favorite_colleges_controller_spec.rb new file mode 100644 index 00000000..0debb0e5 --- /dev/null +++ b/src/app/spec/controllers/favorite_colleges_controller_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe FavoriteCollegesController, type: :controller do + describe "GET #show" do + before(:each) do + @current_user = FactoryBot.create(:user) + end + + context "user logged in" do + before(:each) do + session[:user_id] = @current_user.id + end + + it "has a 200 status code" do + get :show + + expect(response.status).to eq(200) + end + end + + context "user not logged in" do + it "has a 302 status code" do + get :show + + expect(response.status).to eq(302) + end + end + end +end diff --git a/src/app/spec/models/favorite_college_spec.rb b/src/app/spec/models/favorite_college_spec.rb index eda33fd8..2234eeb9 100644 --- a/src/app/spec/models/favorite_college_spec.rb +++ b/src/app/spec/models/favorite_college_spec.rb @@ -1,5 +1,23 @@ require 'rails_helper' RSpec.describe FavoriteCollege, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + context "validation tests - required or not" do + it 'ensures that user is required' do + college = College.new(initials: 'INITIALS', name: 'Universidade Teste 001', city: 'Example City', state: 'ES', country: 'Sample', unit: 'Example', background_color: '#fafafa', font_family: 'Arial', background_image: 'https://townsquare.media/site/366/files/2019/04/Sabaton1.jpg?w=980&q=75', font_color: '#fafafa', request_to_participate: '1', admin_approves_spotted: '1', user: @user) + college.save + + favorite_college = FavoriteCollege.new(college: college).save + + expect(favorite_college).to eq(false) + end + + it 'ensures that college is required' do + user = User.new(:email => "teste001.user@spottapp.com.br", :nickname => "teste001.user", :first_name => "teste001", :last_name => "user", :password => "teste001user", :password_confirmation => "teste001user", :birth_date => "10/10/1998", :city => "São Paulo", :state => "SP", :country => "Brasil") + user.save + + favorite_college = FavoriteCollege.new(user: user).save + + expect(favorite_college).to eq(false) + end + end end From ab51b1a6790b9e308e4fcb8f1f17d20efc974993 Mon Sep 17 00:00:00 2001 From: leogiraldimg Date: Wed, 4 Dec 2019 00:25:43 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Implementando=20testes=20funcionais=20+=20t?= =?UTF-8?q?estes=20unit=C3=A1rios=20-=20likes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/app/controllers/likes_controller.rb | 2 +- .../spec/controllers/likes_controller_spec.rb | 74 +++++++++++++++++++ src/app/spec/factories/likes.rb | 4 +- src/app/spec/models/like_spec.rb | 26 ++++++- 4 files changed, 102 insertions(+), 4 deletions(-) diff --git a/src/app/app/controllers/likes_controller.rb b/src/app/app/controllers/likes_controller.rb index 4d1e8a11..e3845765 100644 --- a/src/app/app/controllers/likes_controller.rb +++ b/src/app/app/controllers/likes_controller.rb @@ -1,5 +1,5 @@ class LikesController < ApplicationController - before_action :find_spotted + before_action :find_spotted, :require_logged_in_user def create if already_liked? diff --git a/src/app/spec/controllers/likes_controller_spec.rb b/src/app/spec/controllers/likes_controller_spec.rb index 6c4e73de..563b0256 100644 --- a/src/app/spec/controllers/likes_controller_spec.rb +++ b/src/app/spec/controllers/likes_controller_spec.rb @@ -1,5 +1,79 @@ require 'rails_helper' RSpec.describe LikesController, type: :controller do + describe "POST #create" do + before(:each) do + @current_user = FactoryBot.create(:user) + @spotted1 = FactoryBot.create(:spotted) + end + context "user logged in" do + before(:each) do + session[:user_id] = @current_user.id + end + + it "redirected to spotted_path" do + post :create, params: {spotted_id: @spotted1.id, origin: "modal"} + + expect(response).to redirect_to(college_spotted_path(@spotted1.college.id, @spotted1.id)) + end + + it "redirected to spotteds_path" do + post :create, params: {spotted_id: @spotted1.id} + + expect(response).to redirect_to(college_spotteds_path(@spotted1.college)) + end + + it "already_liked" do + post :create, params: {spotted_id: @spotted1.id, origin: "modal"} + post :create, params: {spotted_id: @spotted1.id, origin: "modal"} + + expect(flash[:notice]).to match(/You can't like more than once*/) + end + end + + context "user not logged in" do + context "user not logged in" do + it "has a 302 status code" do + post :create, params: {spotted_id: @spotted1.id, origin: "modal"} + + expect(response.status).to eq(302) + end + end + end + end + + describe "DELETE #destroy" do + before(:each) do + @current_user = FactoryBot.create(:user) + @spotted1 = FactoryBot.create(:spotted) + @like = FactoryBot.create(:like) + end + + context "user logged in" do + before(:each) do + session[:user_id] = @current_user.id + end + + it "redirected to spotted_path" do + delete :destroy, params: {spotted_id: @spotted1.id, id: @like.id, origin: "modal"} + + expect(response).to redirect_to(college_spotted_path(@spotted1.college.id, @spotted1.id)) + end + + it "redirected to spotteds_path" do + delete :destroy, params: {spotted_id: @spotted1.id, id: @like.id} + + expect(response).to redirect_to(college_spotteds_path(@spotted1.college)) + end + end + + context "user not logged in" do + it "has a 302 status code" do + delete :destroy, params: {spotted_id: @spotted1.id, id: @like.id, origin: "modal"} + + expect(response.status).to eq(302) + end + end + end end diff --git a/src/app/spec/factories/likes.rb b/src/app/spec/factories/likes.rb index 0d17067a..6972dc24 100644 --- a/src/app/spec/factories/likes.rb +++ b/src/app/spec/factories/likes.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :like do - user { nil } - spotted { nil } + user { User.first || association(:user) } + spotted { Spotted.first || association(:spotted) } end end diff --git a/src/app/spec/models/like_spec.rb b/src/app/spec/models/like_spec.rb index 76ea93df..6f8385ea 100644 --- a/src/app/spec/models/like_spec.rb +++ b/src/app/spec/models/like_spec.rb @@ -1,5 +1,29 @@ require 'rails_helper' RSpec.describe Like, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + context "validation tests - required or not" do + it 'ensures user is required' do + user = User.new(:email => "teste001.user@spottapp.com.br", :nickname => "teste001.user", :first_name => "teste001", :last_name => "user", :password => "teste001user", :password_confirmation => "teste001user", :birth_date => "10/10/1998", :city => "São Paulo", :state => "SP", :country => "Brasil") + user.save + + college = College.new(initials: 'INITIALS', name: 'Universidade Teste 001', city: 'Example City', state: 'ES', country: 'Sample', unit: 'Example', background_color: '#fafafa', font_family: 'Arial', background_image: 'https://townsquare.media/site/366/files/2019/04/Sabaton1.jpg?w=980&q=75', font_color: '#fafafa', request_to_participate: '1', admin_approves_spotted: '1', user: @user) + college.save + + spotted = Spotted.new(:content => "Spotted Teste", :image => "https://townsquare.media/site/366/files/2019/04/Sabaton1.jpg?w=980&q=75", :user => user, :college => college) + spotted.save + + like = Like.new(spotted: spotted).save + + expect(like).to eq(false) + end + + it 'ensures spotted is required' do + user = User.new(:email => "teste001.user@spottapp.com.br", :nickname => "teste001.user", :first_name => "teste001", :last_name => "user", :password => "teste001user", :password_confirmation => "teste001user", :birth_date => "10/10/1998", :city => "São Paulo", :state => "SP", :country => "Brasil") + user.save + + like = Like.new(user: user).save + + expect(like).to eq(false) + end + end end