1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
# Custom RuboCop Cops
TestProf comes with the [RuboCop](https://github.com/rubocop/rubocop) cops that help you write more performant tests.
To enable them, add `test-prof` in your RuboCop configuration:
```yml
# .rubocop.yml
plugins:
- 'test-prof'
```
Or you can just use it dynamically:
```sh
bundle exec rubocop --plugin 'test-prof' --only RSpec/AggregateExamples
```
> [!NOTE]
> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
To configure cops to your needs:
```yml
RSpec/AggregateExamples:
AddAggregateFailuresMetadata: false
```
## RSpec/AggregateExamples
This cop encourages you to use one of the greatest features of the recent RSpec – aggregating failures within an example.
Instead of writing one example per assertion, you can group _independent_ assertions together, thus running all setup hooks only once.
That can dramatically increase your performance (by reducing the total number of examples).
Consider an example:
```ruby
# bad
it { is_expected.to be_success }
it { is_expected.to have_header("X-TOTAL-PAGES", 10) }
it { is_expected.to have_header("X-NEXT-PAGE", 2) }
its(:status) { is_expected.to eq(200) }
# good
it "returns the second page", :aggregate_failures do
is_expected.to be_success
is_expected.to have_header("X-TOTAL-PAGES", 10)
is_expected.to have_header("X-NEXT-PAGE", 2)
expect(subject.status).to eq(200)
end
```
Auto-correction will typically add `:aggregate_failures` to examples, but if your project enables it globally, or selectively by e.g. deriving metadata from file location, you may opt-out of adding it using `AddAggregateFailuresMetadata` config option.
This cop supports auto-correct feature, so you can automatically refactor you legacy tests!
**NOTE**: `its` examples shown here have been deprecated as of RSpec 3, but users of the [rspec-its gem](https://github.com/rspec/rspec-its) can leverage this cop to cut out that dependency.
**NOTE**: auto-correction of examples using block matchers, such as `change` is deliberately not supported.
|