Skip to content

Commit 61679fa

Browse files
committed
Improve message and description of FactoryBot/FactoryClassName
When I saw the cop's message first, I did not know how to fix my code. This modification aims to increase the message's readability by explicitly describing "string" and "constant". Next, I could not understand also why the cop warned when I saw its document. So, this also adds the cop's rationale to its document. This rationale is based on the PR #839 description.
1 parent 0763ad7 commit 61679fa

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
* Improve message and description of `FactoryBot/FactoryClassName`. ([@ybiquitous][])
6+
57
## 1.37.0 (2019-11-25)
68

79
* Implement `RSpec/DescribedClassModuleWrapping` to disallow RSpec statements within a module. ([@kellysutton][])
@@ -466,3 +468,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
466468
[@kellysutton]: https://github.com/kellysutton
467469
[@mkrawc]: https://github.com/mkrawc
468470
[@jfragoulis]: https://github.com/jfragoulis
471+
[@ybiquitous]: https://github.com/ybiquitous

lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ module RSpec
66
module FactoryBot
77
# Use string value when setting the class attribute explicitly.
88
#
9+
# This cop would promote faster tests by lazy-loading of
10+
# application files. Also, this could help you suppress potential bugs
11+
# in combination with external libraries by avoiding a preload of
12+
# application files from the factory files.
13+
#
914
# @example
1015
# # bad
1116
# factory :foo, class: Foo do
@@ -15,7 +20,8 @@ module FactoryBot
1520
# factory :foo, class: 'Foo' do
1621
# end
1722
class FactoryClassName < Cop
18-
MSG = "Pass '%<class_name>s' instead of %<class_name>s."
23+
MSG = "Pass '%<class_name>s' string instead of `%<class_name>s` " \
24+
'constant.'
1925

2026
def_node_matcher :class_name, <<~PATTERN
2127
(send _ :factory _ (hash <(pair (sym :class) $(const ...)) ...>))

manual/cops_factorybot.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ Enabled | Yes
8686

8787
Use string value when setting the class attribute explicitly.
8888

89+
This cop would promote faster tests by lazy-loading of
90+
application files. Also, this could help you suppress potential bugs
91+
in combination with external libraries by avoiding a preload of
92+
application files from the factory files.
93+
8994
### Examples
9095

9196
```ruby

spec/rubocop/cop/rspec/factory_bot/factory_class_name_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
it 'flags passing a class' do
88
expect_offense(<<~RUBY)
99
factory :foo, class: Foo do
10-
^^^ Pass 'Foo' instead of Foo.
10+
^^^ Pass 'Foo' string instead of `Foo` constant.
1111
end
1212
RUBY
1313

@@ -20,7 +20,7 @@
2020
it 'flags passing a class from global namespace' do
2121
expect_offense(<<~RUBY)
2222
factory :foo, class: ::Foo do
23-
^^^^^ Pass 'Foo' instead of Foo.
23+
^^^^^ Pass 'Foo' string instead of `Foo` constant.
2424
end
2525
RUBY
2626

@@ -33,7 +33,7 @@
3333
it 'flags passing a subclass' do
3434
expect_offense(<<~RUBY)
3535
factory :foo, class: Foo::Bar do
36-
^^^^^^^^ Pass 'Foo::Bar' instead of Foo::Bar.
36+
^^^^^^^^ Pass 'Foo::Bar' string instead of `Foo::Bar` constant.
3737
end
3838
RUBY
3939

@@ -55,7 +55,7 @@
5555
it 'flags passing a class' do
5656
expect_offense(<<~RUBY)
5757
factory :foo, class: Foo
58-
^^^ Pass 'Foo' instead of Foo.
58+
^^^ Pass 'Foo' string instead of `Foo` constant.
5959
RUBY
6060

6161
expect_correction(<<~RUBY)

0 commit comments

Comments
 (0)