Skip to content

Commit 2342f39

Browse files
authored
Disallow assertionless tests in Active Model
This commit adds an `after_teardown` logic to the `activemodel` test suite which ensures that performed test incremented the `assertions` counter at least once. Otherwise it raises an `AssertionlessTest` error. This leads to a requirement for tests to be verbose about assertions even if technically it may not be needed. For example a test like: ```ruby def test_submitting_a_review_doesnt_raise review.submit! end ``` will have to at least become ```ruby def test_submitting_a_review_doesnt_raise assert_nothing_raised { review.submit! } end ``` or preferably it should perform a semantically meaningful assertion that will imply not exception being raised, for example: ```ruby def test_submitting_a_review_doesnt_raise review.submit! assert_not_nil review.submitted_at end ``` Overall while the requirement is being defensive it improves readability of the tests along with ensuring that we will never end up having tests that test nothing.
1 parent 2fa766d commit 2342f39

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

activemodel/test/cases/attribute_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,16 @@ def serialize_cast_value(value)
145145
end
146146

147147
test "duping does not eagerly type cast if we have not yet type cast" do
148-
@type.define_singleton_method(:deserialize) { flunk }
149-
attribute = Attribute.from_database(nil, "a value", @type)
148+
deserialize_called = false
149+
deserialize_called_with = nil
150+
@type.define_singleton_method(:deserialize) do |value|
151+
deserialize_called_with = value
152+
deserialize_called = true
153+
end
154+
attribute = Attribute.from_database(nil, "my_attribute_value", @type)
150155

151156
attribute.dup
157+
assert_not deserialize_called, "deserialize should not have been called, but was called with #{deserialize_called_with}"
152158
end
153159

154160
class MyType

activemodel/test/cases/helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
class ActiveModel::TestCase < ActiveSupport::TestCase
1717
include ActiveSupport::Testing::MethodCallAssertions
1818

19+
class AssertionlessTest < StandardError; end
20+
21+
def after_teardown
22+
super
23+
24+
raise AssertionlessTest, "No assertions made." if passed? && assertions.zero?
25+
end
26+
1927
private
2028
# Skips the current run on JRuby using Minitest::Assertions#skip
2129
def jruby_skip(message = "")

0 commit comments

Comments
 (0)