Skip to content

Improve consistency with official controller spec style #2799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged

Conversation

i7an
Copy link
Contributor

@i7an i7an commented Sep 9, 2024

Some feature specs use a controller spec style that is not the "official" one.

# bad?
describe "GET #index" do
  subject { get :index }

  it "renders the index template" do
    expect(subject).to render_template(:index)
  end
end

# vs.

# good?
describe "GET #index" do
  it "renders the index template" do
    get :index

    expect(response).to render_template(:index)
  end
end

Looking at the code base, subject { get :index } is not used anywhere else. It seems to me that this style has questionable semantics, so aligning the the feature spec content with the common approach for controller specs makes sense to me.

In general, I'd like to hear your opinion on using the get, post, patch call as a subject.

@pirj
Copy link
Member

pirj commented Sep 9, 2024

I don’t quite see the problem here. What do you mean by questionable semantics?

@i7an
Copy link
Contributor Author

i7an commented Sep 9, 2024

As I understand subject, it should be an object that you test. Controller tests usually test response or side-effects of invoking a controller action. Thus putting get, post, patch might be a misuse of subject as you are interested more in side-effects, as if calling subject here is like calling a before block.

Also, I did not find many uses of this pattern, which made me wonder why.

Having said that, I do not have a strong opinion and created this PR basically to find out what is wrong and what is right. However I lean towards eliminating the discrepancy in styles.

UPD. Related? comment from you from a while back.
rspec/rspec-expectations#1106 (comment)

I clearly understand that the composition of those two kinds of expectations when checking side effects of an HTTP requests is non-trivial.

@pirj
Copy link
Member

pirj commented Sep 9, 2024

I see.

Rails bends typical RSpec patterns to its needs. Do we test the response, or the interaction here, where the response is the returned result?
Styles of writing specs differ, and we don’t want to impose any specific style as one and only.

I like your suggested change, and I’m inclined to merge it.

However, none of the docs here dictate the style on how to write specs.

@JonRowe
Copy link
Member

JonRowe commented Sep 9, 2024

Our documentation isn't always consistent because its been written at different times and often in different styles, that said we don't typically encourage bare subjects anymore, more typical would be subject(:action) { ... } to make it clear its not the response method.

Also although there is a side effect in that response is changed by get etc, those methods also return the response, which is why it works at all, so I'd actually say subject is fine here.

If you want to name the subject here I'm on board with it but otherwise I think it should be closed to avoid churn.

(An aside, technically controller specs are soft-deprecated in favour of request specs due to Rails decisions).

@pirj pirj merged commit 46726b8 into rspec:main Sep 9, 2024
17 checks passed
@pirj
Copy link
Member

pirj commented Sep 9, 2024

Thank you!

@JonRowe
Copy link
Member

JonRowe commented Sep 9, 2024

Reverted because as above:

If you want to name the subject here I'm on board with it but otherwise I think it should be closed to avoid churn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants