Why is it a code smell to use any_instance_of in Rspec? -
there aren't many resources online closest answer on relishapp.com website:
the rspec-mocks api designed individual object instances, feature operates on entire classes of objects. result there semantically confusing edge cases. example, in expect_any_instance_of(widget).to receive(:name).twice isn't clear whether specific instance expected receive name twice, or if 2 receives total expected. (it's former.)
but if using context/describe blocks correctly scope use case problem not exist @ all.
are there negatives using this?
thanks!
although using any_instance appears handy , easy use, cause confusion test suite grows. method invocation expectations used unit style tests should have complete control on dependencies used test subject. if you're using expect_any_instance_of(widget), implies you're not stubbing/mocking dependencies, , instead allowing code outside unit executed part of test, defeats purpose of unit testing. higher coverage tests, i.e., acceptance/integration, shouldn't setting expectations on objects, should confirming system has done requested, either checking output on webpage, or confirming data saved disk.
Comments
Post a Comment