4 ms·
Honest question. What's the value of a test like this? class MyObjectBuilder def create(object) if validator.valid?(object) factory
by ryanbrunner 10y ago
Honest question. What's the value of a test like this?
class MyObjectBuilder
def create(object)
if validator.valid?(object)
factory.save(object)
else
raise "invalid"
end
end
end
def object_builder_test_success
object_builder.mock(validator, mock_validator)
object_builder.mock(factory, mock_factory)
expect_method_call(validator.validate).return(true)
expect_method_call(factory.save).return(true)
object_builder.create(object)
end
def object_builder_test_failure
object_builder.mock(validator, mock_validator)
object_builder.mock(factory, mock_factory)
expect_method_call(validator.validate).return(false)
assert_raise(object_builder.create(object))
end
This test is both extremely typical for strong advocates of TDD and "testable" designs, and also almost completely useless in my mind. The test is literally a more pained version of the implementation of the method (not to mention that there's typically a lot more ceremony around setting up mocks than my pseudocode indicates)
It adds value in the sense that if someone randomly types garbage into that file it will break, but it acts as a barrier to refactoring or business requirements change, as pretty much nothing but the exact implementation of the method will satisfy the test, and offers no documentation benefit over the code itself.
- billsix 10y agoLol I was on a Ruby project like this, I said these tests are tautologies since we're verbosely rewriting the implementation as a test. My yearly review from that project? "Bill does not understand unit testing" Nobody gets fired for writing too many tests.
- gwright 10y agoThere is no value in a test like this. This is basically a test of a precondition (object must be valid). In fact there is not much value in the entire if statement since it is also testing a precondition. That leaves: def create(object) factory.save(object) end which is just simple delegation. Perhaps just inline the call to create and get rid of the entire class, method, and tests. It is a waste of time testing for a pre-condition. Ensuring a pre-condition is the responsibility of the caller and tests in that part of the code, or integration tests, should be responsible for identifying problems in that regard.