Skip to content

Commit

Permalink
Add tests for models
Browse files Browse the repository at this point in the history
  • Loading branch information
joyapisi committed Aug 10, 2023
1 parent e49fee0 commit 7d4827b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
29 changes: 28 additions & 1 deletion spec/models/cloth_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
require 'rails_helper'
require 'faker'

RSpec.describe Cloth, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
before(:each) do
@joy = User.create!(name: Faker::Name.unique.name,
email: Faker::Internet.email,
password: 'joy123', password_confirmation: 'joy123')

@group = Group.create!(name: 'Tops', icon: 'some icon', user: @joy)
end

subject { Cloth.new(name: 'Shirt', amount: 25, user: @joy, group: @group) }

before { subject.save }

describe 'Check validity of Cloths' do
it 'should check validity of name' do
subject.name = nil
expect(subject).to_not be_valid
end

it 'should check validity of amount' do
subject.amount = nil
expect(subject).to_not be_valid
end

it 'should check validity to be saved' do
expect(subject).to be_valid
end
end
end
27 changes: 26 additions & 1 deletion spec/models/group_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
require 'rails_helper'
require 'faker'

RSpec.describe Group, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
before(:each) do
@joy = User.create!(name: Faker::Name.unique.name,
email: Faker::Internet.email,
password: 'joy123', password_confirmation: 'joy123')
end

subject { Group.new(name: 'Tops', icon: 'some icon', user: @joy) }

before { subject.save }

describe 'Check validity of Model' do
it 'should check validity of name' do
subject.name = nil
expect(subject).to_not be_valid
end

it 'should check validity of icon' do
subject.icon = nil
expect(subject).to_not be_valid
end

it 'should check validity to be saved' do
expect(subject).to be_valid
end
end
end
41 changes: 40 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
require 'rails_helper'
require 'faker'
require 'shoulda/matchers'

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
describe 'Check user validity' do
subject do
User.new(name: Faker::Name.unique.name,
email: Faker::Internet.email,
password: 'joy123', password_confirmation: 'joy123')
end

it 'should be valid with correct attributes' do
expect(subject).to be_valid
end

it 'should not be valid without a name' do
subject.name = nil
expect(subject).to_not be_valid
end

it 'should not be valid without an email' do
subject.email = nil
expect(subject).to_not be_valid
end
end

context 'associations' do
it { should have_many(:clothes).dependent(:destroy).class_name('Cloth').with_foreign_key(:user_id) }
it { should have_many(:groups).dependent(:destroy).class_name('Group').with_foreign_key(:user_id) }
end

context 'validations' do
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:email).case_insensitive }
it { should validate_presence_of(:password) }
it { should validate_length_of(:password).is_at_least(6) }
it { should validate_confirmation_of(:password) }
end

context 'devise modules' do
it { should validate_presence_of(:password_confirmation) }
end
end

0 comments on commit 7d4827b

Please sign in to comment.