rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

Cop idea: [Factory girl] Enforce consistent usage of parentheses in factory girl methods

Open Darhazer opened this issue 8 years ago • 7 comments
trafficstars

I'm working on a code base where I have both styles of using factory: create(:user, name: 'John') and create :user, name: 'John; it will be good if one of those styles is required.

Note: parentheses may be required in nested associations no matter the style, like in: create :account, user: create(:user, name 'John')

Darhazer avatar May 26 '17 07:05 Darhazer

I feel this is more general style choice of whether method calls should use parentheses or not. Perhaps it would be better to be have this in RuboCop itself?

andyw8 avatar May 27 '17 01:05 andyw8

IMO it's OK to have different method call style for different DSLs. Syle/MethodCallWithArgsParentheses looks for such, but we disable it in the specs, as I can't imagine writting it('some description') or even allow(Foo).to(receive(:bar))

Darhazer avatar Jun 03 '17 16:06 Darhazer

I would love a cop enforcing this. There is usually no standard when regarding factorybot DSL

Drowze avatar Jun 05 '19 14:06 Drowze

I think this rule applies to FactoryGirl.

So if there's no such cop in RuboCop itself (couldn't find one quickly), it might be a good idea to implement it there.

pirj avatar Jun 05 '19 16:06 pirj

I don't know if the rule accounts for this at all, but I usually use parentheses with a "dsl" unless it's in a class context / macro. So, for the validates example, I usually write it as the guide recommends, but for almost all the rspec methods that aren't describe, context, it, or to, for instance, i typically use parentheses. Like @Darhazer is saying, I don't think a universal rule necessarily matches all DSLs.

Personally, I always write factories with parentheses and that seems like the better default to me (disclaimer: i'm trending towards more parens in most places), but this could easily be configurable and I expect it will be somewhat controversial either way.

dgollahon avatar Jun 18 '19 06:06 dgollahon

Any update with this?

vmakoed avatar Jul 01 '20 08:07 vmakoed

@vmakoed Pull request is always welcome!

I guess this cop should have a style option whether to set parantheses or not.

pirj avatar Jul 01 '20 09:07 pirj