Sha256: c0c0a1a73a172fa7e1f955fe60e44cfd696d109de65be2dec32d7dbfccd8ce16

Contents?: true

Size: 1.26 KB

Versions: 20

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
FactoryBot.define do
  factory :user do
    display_name { FFaker::Name.name }
    email { FFaker::Internet.email }
    password { "123456" }

    transient do
      # Allow for custom groups when a user is instantiated.
      # @example FactoryBot.create(:user, groups: 'admin')
      groups { [] }
    end

    after(:build) do |user, evaluator|
      # In case we have the instance but it has not been persisted
      ::RSpec::Mocks.allow_message(user, :groups).and_return(Array.wrap(evaluator.groups))
      # Given that we are stubbing the class, we need to allow for the original to be called
      ::RSpec::Mocks.allow_message(user.class.group_service, :fetch_groups).and_call_original
      # We need to ensure that each instantiation of the admin user behaves as expected.
      # This resolves the issue of both the created object being used as well as re-finding the created object.
      ::RSpec::Mocks.allow_message(user.class.group_service, :fetch_groups).with(user: user).and_return(Array.wrap(evaluator.groups))
    end

    factory :admin do
      groups { ['admin'] }

      after(:create) do |user, _evaluator|
        role = Role.find_or_create_by(name: 'admin')
        role.users << user
        role.save
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
zizia-6.0.1 spec/factories/user.rb
zizia-6.0.0 spec/factories/user.rb
zizia-5.5.0 spec/factories/user.rb
zizia-5.4.0 spec/factories/user.rb
zizia-5.3.0 spec/factories/user.rb
zizia-5.2.0 spec/factories/user.rb
zizia-5.1.0 spec/factories/user.rb
zizia-5.0.1 spec/factories/user.rb
zizia-5.0.0 spec/factories/user.rb
zizia-4.6.0.alpha.01 spec/factories/user.rb
zizia-4.5.4.alpha.01 spec/factories/user.rb
zizia-4.5.3.alpha.01 spec/factories/user.rb
zizia-4.5.2.alpha.01 spec/factories/user.rb
zizia-4.5.1.alpha.01 spec/factories/user.rb
zizia-4.4.1.alpha.01 spec/factories/user.rb
zizia-4.4.0.alpha.01 spec/factories/user.rb
zizia-4.3.1.alpha.01 spec/factories/user.rb
zizia-4.2.0.alpha.01 spec/factories/user.rb
zizia-4.1.0.alpha.01 spec/factories/user.rb
zizia-4.0.4.alpha.01 spec/factories/user.rb