Skip to content

Commit 8359ac9

Browse files
committed
docs: enhance assert_changes and assert_no_changes documentation
Added comprehensive examples demonstrating case equality and type checking capabilities of the assert_changes and assert_no_changes methods. The examples show how these methods can be used with exact value matches, regular expressions, and class type checks using the case equality (===) operator.
1 parent 885734a commit 8359ac9

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

activesupport/lib/active_support/testing/assertions.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,24 @@ def assert_no_difference(expression, message = nil, &block)
181181
#
182182
# The keyword arguments +:from+ and +:to+ can be given to specify the
183183
# expected initial value and the expected value after the block was
184-
# executed.
184+
# executed. The comparison is done using case equality (===), which means
185+
# you can specify patterns or classes:
185186
#
187+
# # Exact value match
186188
# assert_changes :@object, from: nil, to: :foo do
187189
# @object = :foo
188190
# end
189191
#
192+
# # Case equality
193+
# assert_changes -> { user.token }, to: /\w{32}/ do
194+
# user.generate_token
195+
# end
196+
#
197+
# # Type check
198+
# assert_changes -> { current_error }, from: nil, to: RuntimeError do
199+
# raise "Oops"
200+
# end
201+
#
190202
# An error message can be specified.
191203
#
192204
# assert_changes -> { Status.all_good? }, 'Expected the status to be bad' do
@@ -238,12 +250,24 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
238250
# end
239251
#
240252
# Provide the optional keyword argument +:from+ to specify the expected
241-
# initial value.
253+
# initial value. The comparison is done using case equality (===), which means
254+
# you can specify patterns or classes:
242255
#
256+
# # Exact value match
243257
# assert_no_changes -> { Status.all_good? }, from: true do
244258
# post :create, params: { status: { ok: true } }
245259
# end
246260
#
261+
# # Case equality
262+
# assert_no_changes -> { user.token }, from: /\w{32}/ do
263+
# user.touch
264+
# end
265+
#
266+
# # Type check
267+
# assert_no_changes -> { current_error }, from: RuntimeError do
268+
# retry_operation
269+
# end
270+
#
247271
# An error message can be specified.
248272
#
249273
# assert_no_changes -> { Status.all_good? }, 'Expected the status to be good' do

0 commit comments

Comments
 (0)