Refactor FactoryBot factories

This commit is contained in:
Urban Rotnik 2019-01-09 18:30:33 +01:00
parent 47cc6a9d10
commit d738169120
7 changed files with 64 additions and 58 deletions

View file

@ -1,26 +1,19 @@
# frozen_string_literal: true
FactoryBot.define do
factory :experiment do
name { Faker::Name.unique.name }
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.'
end
factory :experiment_one, class: Experiment do
name 'My Experiment One'
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.'
association :project, factory: :project
association :created_by, factory: :user, email: Faker::Internet.email
association :last_modified_by,
factory: :user,
email: Faker::Internet.email
end
factory :experiment_two, class: Experiment do
name Faker::Name.name
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.'
association :project, factory: :project
association :created_by, factory: :user, email: Faker::Internet.email
association :last_modified_by,
factory: :user,
email: Faker::Internet.email
factory :experiment, class: Experiment do
transient do
user { create :user }
end
sequence(:name) { |n| "Experiment-#{n}" }
description { Faker::Lorem.sentence }
created_by { user }
last_modified_by { user }
project { create :project, created_by: user }
factory :experiment_with_tasks do
after(:create) do |e|
create_list :my_module, 3, experiment: e
end
end
end
end

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true
FactoryBot.define do
factory :my_module_group do
experiment { Experiment.first || create(:experiment_two) }
experiment
end
end

View file

@ -1,10 +1,12 @@
# frozen_string_literal: true
FactoryBot.define do
factory :my_module do
name 'My first module'
x 0
y 0
workflow_order 0
experiment { Experiment.first || create(:experiment_one) }
my_module_group { MyModuleGroup.first || create(:my_module_group) }
sequence(:name) { |n| "Task-#{n}" }
x { Faker::Number.between(1, 100) }
y { Faker::Number.between(1, 100) }
workflow_order { 0 }
experiment
my_module_group { create :my_module_group, experiment: experiment }
end
end

View file

@ -1,10 +1,12 @@
# frozen_string_literal: true
FactoryBot.define do
factory :project do
created_by { User.first || association(:project_user) }
team { Team.first || association(:project_team) }
archived false
name 'My project'
visibility 'hidden'
sequence(:name) { |n| "My project-#{n}" }
association :created_by, factory: :user
team { create :team, created_by: created_by }
archived { false }
visibility { 'hidden' }
end
factory :project_user, class: User do
@ -14,11 +16,4 @@ FactoryBot.define do
password 'asdf1243'
password_confirmation 'asdf1243'
end
factory :project_team, class: Team do
created_by { User.first || association(:project_user) }
name 'My team'
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.'
space_taken 1048576
end
end

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :team do
created_by { User.first || create(:user) }
association :created_by, factory: :user
name 'My team'
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.'
space_taken 1048576

View file

@ -1,8 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :user do
full_name 'admin'
initials 'AD'
email Faker::Internet.unique.email
sequence(:email) { |n| "user-#{n}@example.com" }
password 'asdf1243'
password_confirmation 'asdf1243'
current_sign_in_at DateTime.now

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe Project, type: :model do
@ -39,22 +41,30 @@ describe Project, type: :model do
it { should have_many :report_elements }
end
describe 'Should be a valid object' do
let(:user) { create :user }
let(:team) { create :team, created_by: user }
it { should validate_presence_of :visibility }
it { should validate_presence_of :team }
it do
should validate_length_of(:name).is_at_least(Constants::NAME_MIN_LENGTH)
.is_at_most(Constants::NAME_MAX_LENGTH)
describe 'Validations' do
describe '#visibility' do
it { should validate_presence_of :visibility }
end
it 'should have a unique name scoped to team' do
create :project, created_by: user, last_modified_by: user, team: team
project_two = build :project, created_by: user,
last_modified_by: user,
team: team
expect(project_two).to_not be_valid
describe '#team' do
it { should validate_presence_of :team }
end
describe '#name' do
it 'should be at least 2 long and max 255 long' do
should validate_length_of(:name)
.is_at_least(Constants::NAME_MIN_LENGTH)
.is_at_most(Constants::NAME_MAX_LENGTH)
end
it 'should be uniq per project and team' do
first_project = create :project
second_project = build :project,
name: first_project.name,
team: first_project.team
expect(second_project).to_not be_valid
end
end
end
end