module Lopata::ActiveRecord::Methods
To be included in Lopata::Scenario
. The methods may be used in runtime.
Public Instance Methods
Destroy ActiveRecord
objects.
Does nothing if 'keep' mode is enabled:
Lopata.configure do |c| c.keep = true end
@param objects [Array<ActiveRecord::Base, Array<ActiveRecord::Base>, nil>] to be destroyed @see Lopata::Configuration#keep
# File lib/lopata/active_record.rb, line 47 def cleanup(*objects) return if Lopata.configuration.keep objects.flatten.compact.each do |o| begin o.reload.destroy! rescue ::ActiveRecord::RecordNotFound # Already destroyed - skip rescue ::ActiveRecord::InvalidForeignKey # Possible async job created new relationships (e.g. history records). Try again once. o.reload.destroy! end end end
Marks object to be destroyed at the end of scenario
@param object [ActiveRecord::Base] the object to be destoryed at the end of scenario @return the given object, so chains can be build
# File lib/lopata/active_record.rb, line 79 def cleanup_later(object) return nil unless object @created_objects ||= [] @created_objects << object object end
Find ActiveRecord
object of given class by params. Marks the returned object to be destroyed at the end of scenario.
@example
action do # UI actions creating the user @user = find_created(User, username: 'testuser') end it 'created' do expect(@user).to_not be_nil end # No cleanup needed # cleanup :user
@param cls [Class] active record model class @param params [Hash] options for record finding @return [ActiveRecord::Base, nil] the object or nil if not found @see cleanup_later
called on the hood
# File lib/lopata/active_record.rb, line 104 def find_created(cls, params) cleanup_later cls.where(params).take end
Reload ActiveRecord
objects
@example
# use in steps reload @a, @b # instead of @a.reload; @b.reload
@param objects [Array<ActiveRecord::Base, Array<ActiveRecord::Base>, nil>] to be reloaded
# File lib/lopata/active_record.rb, line 71 def reload(*objects) objects.flatten.compact.each(&:reload) end