-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementando testes funcionais + testes unitários - likes
- Loading branch information
1 parent
5ef26f3
commit ab51b1a
Showing
4 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
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
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
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 |
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
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 |
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
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 |