- Your descriptions should read like a sentence beginning with "It". If testing an actual instance method, use
#method_name
in your descriptions. - Use factories when you need to build or create an actual object.
- This
expect
statement doesn't quite match the description. Ensure your description and assertion are the same. - Again, factories.
- If you're going to use the new
expect
syntax, use it consistently. - Most tests should be isolated enough that you only need to make one assertion per test. If the tests are examining the same object, use
subject
for added conciseness. - This should, again, use a Factory. In any case, calling bang methods in poorly-written tests might raise exceptions that obscure the actual reason for a failed test.
- This assertion is not testing what is indicated in the description.
- Don't comment out tests. Alias
xit
to ignored tests in your RSpec config to quickly toggle a test to an ignored state. - Contexts should be descriptive and generally refer to a state or condition. They are not a catch-all organizational tool.
- There is rarely a good reason to use
before(:all)
. Favorlet
andlet!
for simple instantiation of objects instead of instance variables. - There is no need to persist this object. Use mocks/stubs when the object's properties are irrelevant in the testing context.
- This is another opportunity to use
let
. If a part of your test suite requires a complex setup procedure, those tests probably aren't sufficiently isolated. At the very least, move complex setup code into an included module. - Database transactions are slow. Don't persist more objects than you need to. With proper use of factories and mocks, this often means that you don't need to persist anything!
- When creating objects for shared use among tests, make sure all tests in that context actually need those objects, since these objects are repeatedly destroyed and recreated.
- Examine only one behavior per test.
- Use
describe
instead ofcontext
to group tests if referring to an actual method or functionality. - This is another example of a description in need of rewriting.
- Are you testing the functionality of the instance method or the code in the
before(:each)
block? Keep your tests simple and isolated. - Use a
describe '#has_orders'
block to contain tests relating to a single instance method. - If there is a built-in matcher available, like
be_true
, use it.