From bbbbd4e747e9269a0ffd865fa9bbe757c8c7d751 Mon Sep 17 00:00:00 2001 From: peterrobert Date: Wed, 24 Jun 2020 12:36:01 +0300 Subject: [PATCH] created the unit tests --- spec/models/group_spec.rb | 24 ++++++++++++++++++++++++ spec/models/transaction_spec.rb | 31 +++++++++++++++++++++++++++++++ spec/models/user_spec.rb | 31 ++++++++++++++++++------------- spec/support/database_cleaner.rb | 17 +++++++++++++++++ 4 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 spec/models/group_spec.rb create mode 100644 spec/models/transaction_spec.rb create mode 100644 spec/support/database_cleaner.rb diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb new file mode 100644 index 0000000..16dfffa --- /dev/null +++ b/spec/models/group_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' + +RSpec.describe Group, type: :model do + + test_user = User.create!(name: 'peter robert', email: 'b@example.com', password: 'hsjoarty067') + test_group = Group.create!(name: 'test_group', icon: 'https://testicons.com/testicon.png', user_id:test_user.id) + + + it 'should fail to create a group without name' do + ev = Group.create(icon: 'https://testicons.com/testicon.png') + expect(ev.persisted?).to eql(false) + end + + it 'should fail to create a group user' do + ev = Group.create(name: 'test_group',icon: 'https://testicons.com/testicon.png') + expect(ev.persisted?).to eql(false) + end + + it 'should create group' do + ev = Group.create(name: 'test_group',icon: 'https://testicons.com/testicon.png', user_id:test_user.id) + expect(ev.persisted?).to eql(true) + end + +end \ No newline at end of file diff --git a/spec/models/transaction_spec.rb b/spec/models/transaction_spec.rb new file mode 100644 index 0000000..4594f6e --- /dev/null +++ b/spec/models/transaction_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +RSpec.describe Transaction, type: :model do + + test_user = User.create!(name: 'peter robert', email: 'pan@example.com', password: 'hsjoarty067') + test_group = test_user.groups.create!(name: 'test_group', icon: 'https://testicons.com/testicon.png') + test_transaction = Transaction.create!(name: 'test_transaction', amount: 100, user_id: test_user.id, group_id: test_group.id) + + + it 'should fail to create a transaction without amount' do + ev = Transaction.create(name: 'test event') + expect(ev.persisted?).to eql(false) + end + + it 'should fail to create a transaction without name' do + ev = Transaction.create(amount: 200) + expect(ev.persisted?).to eql(false) + end + + it 'should fail to create a transaction without user' do + ev = Transaction.create(name: 'test event', amount: 100, group_id: test_group.id ) + expect(ev.persisted?).to eql(false) + end + + it 'should be saved with all users' do + ev = Transaction.create(name: 'test event', amount: 100, user_id: test_user.id, group_id: test_group.id ) + expect(ev.persisted?).to eql(true) + end + + +end \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6646261..43b101c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,19 +1,24 @@ -RSpec.describe User, type: :model do - context 'validation tests' do - it 'ensures name presence' do - - end +require 'rails_helper' - it 'ensures email presence' do - - end +RSpec.describe User, type: :model do - it 'ensures password presence' do - + test_user = User.create!(name: 'peter robert', email: 'h@example.com', password: 'hsjoarty067') + + it 'should fail to create account without name' do + ev = User.create(email: 'b@example.com', password: 'hsjoarty067') + expect(ev.persisted?).to eql(false) end - it 'ensures confirm password presence' do + it 'should fail to create account without email' do + ev = User.create(name: 'peter robert', password: 'hsjoarty067') + expect(ev.persisted?).to eql(false) + end + it 'should fail to create account without password' do + ev = User.create(name: 'peter robert', email: 'e@example.com') + expect(ev.persisted?).to eql(false) end - end - end \ No newline at end of file + + + +end \ No newline at end of file diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 0000000..79e6011 --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,17 @@ +RSpec.configure do |config| + config.before(:suite) do + DatabaseCleaner.clean_with :truncation, except: %w[ar_internal_metadata] + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end + end \ No newline at end of file