File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
lib/active_support/testing Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ * Add ability to match exception messages to ` assert_raises ` assertion
2
+
3
+ Instead of this
4
+ ``` ruby
5
+ error = assert_raises(ArgumentError ) do
6
+ perform_service(param: ' exception' )
7
+ end
8
+ assert_match(/incorrect param/i , error.message)
9
+ ```
10
+
11
+ you can now write this
12
+ ` ` ` ruby
13
+ assert_raises(ArgumentError, match: /incorrect param/i) do
14
+ perform_service(param: 'exception')
15
+ end
16
+ ` ` `
17
+
18
+ * fatkodima*
19
+
1
20
* Add ` Rails.env.local?` shorthand for ` Rails.env.development? || Rails.env.test?` .
2
21
3
22
* DHH *
Original file line number Diff line number Diff line change @@ -23,6 +23,19 @@ def assert_not(object, message = nil)
23
23
assert !object , message
24
24
end
25
25
26
+ # Asserts that a block raises one of <tt>exp</tt>. This is an enhancement of the
27
+ # standard Minitest assertion method with the ability to test error messages.
28
+ #
29
+ # assert_raises(ArgumentError, match: /incorrect param/i) do
30
+ # perform_service(param: 'exception')
31
+ # end
32
+ #
33
+ def assert_raises ( *exp , match : nil , &block )
34
+ error = super ( *exp , &block )
35
+ assert_match ( match , error . message ) if match
36
+ error
37
+ end
38
+
26
39
# Assertion that the block should not raise an exception.
27
40
#
28
41
# Passes if evaluated code in the yielded block raises no exception.
Original file line number Diff line number Diff line change @@ -28,6 +28,20 @@ def test_assert_not
28
28
assert_equal "custom" , e . message
29
29
end
30
30
31
+ def test_assert_raises_with_match_pass
32
+ assert_raises ( ArgumentError , match : /incorrect/i ) do
33
+ raise ArgumentError , "Incorrect argument"
34
+ end
35
+ end
36
+
37
+ def test_assert_raises_with_match_fail
38
+ assert_raises ( Minitest ::Assertion , match : "Expected /incorrect/i to match \" Wrong argument\" ." ) do
39
+ assert_raises ( ArgumentError , match : /incorrect/i ) do
40
+ raise ArgumentError , "Wrong argument"
41
+ end
42
+ end
43
+ end
44
+
31
45
def test_assert_no_difference_pass
32
46
assert_no_difference "@object.num" do
33
47
# ...
You can’t perform that action at this time.
0 commit comments