Skip to content

Instantly share code, notes, and snippets.

@afmicc
Created October 18, 2020 19:17
Show Gist options
  • Save afmicc/2df97cceefbbe78cf1d5e40b0a2ab887 to your computer and use it in GitHub Desktop.
Save afmicc/2df97cceefbbe78cf1d5e40b0a2ab887 to your computer and use it in GitHub Desktop.
def change_multiple(list, item_field, expected_changes = ->(x) { x })
change_matcher = lambda { |item|
expected_changes.call(
change { item.reload[item_field] }
)
}
first_item = list.first
rest = list[1..-1]
first_matcher = change_matcher.call(first_item)
rest.inject(first_matcher) do |matcher, item|
matcher.and(
change_matcher.call(item)
)
end
end
# Example of use:
it 'updates the question‘s activity' do
expect {
subject
}.to change_multiple(Question.all, :activity_id, ->(x) { x.from(nil).to(Integer) })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment