You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I really appreciate the work that's been done here to develop these guidelines, and the philosophy surrounding it. It has been a source of inspiration for my team (and spirited debate), helping us push towards our own internal standards.
I'd like to suggest a new best practice. Consider the following example.
RSpec.describeInvoicedodescribe'#draft?'docontext'when the status is draft'doit'returns true'doinvoice=create(:invoice)# <-- Problem: Relying on factory defaults for test setup.expect(invoice.draft?).toeq(true)endendcontext'when the status is paid'doit'returns false'doinvoice=create(:invoice,status: :paid)expect(invoice.draft?).toeq(false)endendendend
I've seen many tests like this, where a particular context relies on the default values of a factory for test setup. In my opinion, this violates the philosophy of Even Better Specs.
The test is not self-contained. The test setup relies on a default value set elsewhere (in the factory). If that default changes, this test will break, making the test brittle. This test does not follow the Arrange-Act-Assert patternexplicitly. The test setup that the context depends on happens implicitly using a default value. This can reduce the readability of the code, as I might be surprised to see a lack of test setup corresponding with the test context.
Instead, in keeping with the philosophy, I would suggest explicitly setting up all test data necessary to validate a particular context's setup.
context'when the status is draft'doit'returns true'doinvoice=create(:invoice,status: :draft)expect(invoice.draft?).toeq(true)endend
I am calling this rule don't rely on factory defaults. I look forward to hearing what you think. I'm happy to draft a pull request if this feels worthy to include.
The text was updated successfully, but these errors were encountered:
Hey 👋🏻
I really appreciate the work that's been done here to develop these guidelines, and the philosophy surrounding it. It has been a source of inspiration for my team (and spirited debate), helping us push towards our own internal standards.
I'd like to suggest a new best practice. Consider the following example.
I've seen many tests like this, where a particular context relies on the default values of a factory for test setup. In my opinion, this violates the philosophy of Even Better Specs.
The test is not self-contained. The test setup relies on a default value set elsewhere (in the factory). If that default changes, this test will break, making the test brittle. This test does not follow the Arrange-Act-Assert pattern explicitly. The test setup that the context depends on happens implicitly using a default value. This can reduce the readability of the code, as I might be surprised to see a lack of test setup corresponding with the test context.
Instead, in keeping with the philosophy, I would suggest explicitly setting up all test data necessary to validate a particular context's setup.
I am calling this rule don't rely on factory defaults. I look forward to hearing what you think. I'm happy to draft a pull request if this feels worthy to include.
The text was updated successfully, but these errors were encountered: