Enforce non-admin attribute of user factory

Change in other specs to match the new factory setup.
This commit is contained in:
Aditya Prakash 2016-03-09 21:34:07 +05:30
parent b18e302224
commit 0619eb7963
2 changed files with 28 additions and 3 deletions

View file

@ -1,4 +1,18 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
# It is a feature of our app that first signed up user is admin. This property
# is set in a before create callback `setup_role` in user model.
# We want to override this behavior when we create user factories. We want
# `create(:user)` to create non-admin user by default. We are enforcing it
# with `after create` blocks in following factories.
#
# Following commands won't work:
# `create(:user, is_admin: true)`
# `create(:admin, is_admin: false)`
#
# For a non-admin user, use: `create(:user)`
# For an admin user, use: `create(:admin)
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "example#{n}@example.com" }
@ -16,8 +30,19 @@ FactoryGirl.define do
gravida.
EOS
after(:create) do |user|
user.is_admin = false
# save with bang cause we want change in DB and not just in object instance
user.save!
end
factory :admin do
is_admin true
# admin factory needs its own after create block or else after create
# of user factory will override `is_admin` value.
after(:create) do |user|
user.is_admin = true
user.save!
end
end
end
end