Improved naming definitely helps. The name “Mystery Guest” comes from [XUnitPatterns]( Obscure Test at XUnitPatterns.com Guest):
The test reader is not able to see the cause and effect between fixture and verification logic because part of it is done outside the Test Method.
In XUnitPatterns, parlance, “fixture” means the setup phase of the test, which is anything before the method-under-test is executed, and “Test Method” is an RSpec it
block.
Their first recommendation is to try inlining as much setup as possible:
Using a Fresh Fixture built using Inline Setup is the obvious solution for Mystery Guest.
Basically, you’d ideally have user = something
in your test somewhere, and the “something” would start out being explicit as possible.
XUnitPatterns also recommend extracting methods specifically for removing irrelevant information:
To avoid Irrelevant Information, we may want to hide the details of the construction behind one or more evocatively-named Creation Methods that append to the file’s contents.
In Rails, you can use factory_girl, which is a library specifically for defining Creation Methods.
The last recommendation they have is to use well-named methods:
If we must use a Shared Fixture or Implicit Setup, we should consider using evocatively named Finder Methods (see Test Utility Method) to access the objects in the fixture. If we must use external resources such as files, we should put them into a special folder/directory and give them names that make it obvious what kind of data they hold.
Essentially, the recommendation there is to use a naked method call only if there’s no other relevant information (any user will do) or there’s no better option (you must extract the fixture to eliminate duplication, test slowness, or something else).
In this case, I agree with all of the recommendations made by XUnitPatterns.
Summary: Naming things well is always useful, but removing relevant test setup from the test should be avoided until there’s a stronger reason to pull it out.