- Avoid the
privatekeyword in specs. - Avoid checking boolean equality directly. Instead, write predicate methods and use appropriate matchers. Example.
- Prefer
eqto==in RSpec. - Separate setup, exercise, verification, and teardown phases with newlines.
- Use RSpec's
expectsyntax. - Use RSpec's
allowsyntax for method stubs. - Use
not_toinstead ofto_notin RSpec expectations. - Prefer the
have_cssmatcher to thehave_selectormatcher in Capybara assertions. - Avoid
any_instanceinrspec-mocksandmocha; Prefer dependency injection. - Avoid
its,specify, andbeforein RSpec. - Avoid
let(orlet!) in RSpec. Prefer extracting helper methods, but do not re-implement the functionality oflet. Example. - Avoid using
subjectexplicitly inside of an RSpecitblock. Example. - Avoid using instance variables in tests.
- Disable real HTTP requests to external services with
WebMock.disable_net_connect!. - Don't test private methods.
- Test background jobs with a
Delayed::Jobmatcher. - Use stubs and spies (not mocks) in isolated tests.
- Use a single level of abstraction within
itexamples. - Use an
itexample or test method for each execution path through the method. - Use assertions about state for incoming messages.
- Use stubs and spies to assert you sent outgoing messages.
- Use a Fake to stub requests to external services.
- Use integration tests to execute the entire app.
- Use non-SUT methods in expectations when possible.
- Use the most specific selectors available.
- Don't locate elements with CSS selectors or
[id]attributes. - Use accessible names and descriptions to locate elements, to interact with form controls, buttons, and links, or to scope blocks of actions and assertions.
- Avoid
itblock descriptions that add no information, such as "successfully." - Avoid
itblock descriptions that repeat the top-leveldescribeblock description. - Place helper methods for system specs directly in a top-level
Systemmodule. - Use names like
ROLE_ACTION_spec.rb, such asuser_changes_password_spec.rb, for system spec file names. - Use only one
describeblock per system spec file. - Use
itblock descriptions that describe the success and failure paths. - Use spec/system directory to store system specs.
- Use spec/support/system for support code related to system specs.
- Don't assert an element's state with
[class]or[data-*]attributes. - Use WAI-ARIA States and Properties (i.e.
[role]or[aria-*]attributes) when asserting an element's state. - Prefer assertions about implicit semantics and built-in attributes (e.g. an
<input type="checkbox">element and[checked], an<option>element and[selected]) over WAI-ARIA States and Properties (e.g. a<button>element and[aria-checked="true"], a<div>element and[aria-selected="true"]).
system specs were previously called feature specs and lived in
spec/features
- Order
factories.rbcontents: sequences, traits, factory definitions. - Order factory attributes: implicit attributes, explicit attributes, child factory definitions. Each section's attributes are alphabetical.
- Order factory definitions alphabetically by factory name.
- Don't prefix
itblock descriptions withshould. Use Imperative mood instead. - Use
subjectblocks to define objects for use in one-line specs. Example. - Put one-liner specs at the beginning of the outer
describeblocks. - Use
.methodto describe class methods and#methodto describe instance methods. - Use
contextto describe testing preconditions. - Use
describe '#method_name'to group tests by method-under-test - Use a single, top-level
describe ClassNameblock. - Order validation, association, and method tests in the same order that they appear in the class.