Skip to content

Commit

Permalink
Implementando testes funcionais + testes unitários - likes
Browse files Browse the repository at this point in the history
  • Loading branch information
leogiraldimg committed Dec 4, 2019
1 parent 5ef26f3 commit ab51b1a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class LikesController < ApplicationController
before_action :find_spotted
before_action :find_spotted, :require_logged_in_user

def create
if already_liked?
Expand Down
74 changes: 74 additions & 0 deletions src/app/spec/controllers/likes_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/app/spec/factories/likes.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 25 additions & 1 deletion src/app/spec/models/like_spec.rb
Original file line number Diff line number Diff line change
@@ -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 => "[email protected]", :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 => "[email protected]", :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

0 comments on commit ab51b1a

Please sign in to comment.