-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Where to test non-client-facing behaviors from controller actions? #2373
Comments
That's a question that was bothering me as well for a long time. Do you think it makes sense to start a discussion on this topic in the Rails discussion group. Related: rubocop/rspec-style-guide#113 - I've been postponing working on this since there's lack of information/best practices around that. |
It's confusing why we mention
|
I think this is a good idea :) |
I disagree here, sending notification emails is certainly client facing behaviour, and a background job triggered by a client might not have a visible side effect but is still a side effect of client facing behaviour.
That link was last updated in 2011, might be time to update it.
I suspect thats related to controller tests being provided by the older Rails / |
There are other background jobs than sending email to the user. It could be an email to the company staff of the application which the client will never notice. It could be a call to an external API of which the result is invisible to the client. If your stance is to test this in integration tests, that's one thing, but you can't possibly call them client-facing. |
Client doesn't mean human. |
I am aware. |
I'm open to improving the wording, but client facing here means "behaviour triggered by the outside world". |
My understanding of client-facing is the provided API, whether that's a JSON endpoint or a form in a HTML page. My interpretation of "client-facing behavior" is any behavior which is communicated to that client. Not just triggered by the client. I could be wrong. I have been searching for a clear definition of the term, but it's not easy to find. Given that, I still would not check for background jobs being executed in system tests, which I definitly consider to be user-facing. But I don't really see an advantage in moving tests of side-effects from the controller to a request spec. Or would you advocate checking side-effects in system tests? |
Ah you are missing a few pieces of information here, which comes back to my point about improving the documentation.
Request specs do largely the same job as controller specs, the main difference being they run through the entirety of the rack stack, where as controller specs did not. Thus the advantage in moving them is future proofing, having a more realistic behaviour, and being better integration tests (controller specs gave the impression of being a unit test when they were nothing of the sort, they were a partial integration of the rack stack). At some point controller specs will go away entirely because the mechanisms supporting them will be unsupported. |
I am currently bringing up to date a large test suite, and I've seen this soft deprecation of the controller specs mentioned elsewhere, but mostly in rails 5.x discussions. In the rails 6 guide, I can't find a reference for this! I'm looking at the guide for v6.10 https://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers What I can see is that the Rails team calls them functional tests instead of controller tests. Could this naming convention part of the deprecation? (Old controller tests are no more, functional tests are the new norm?) If this is the case, then there is no further deprecation coming for RSpec's controller specs, as they are just wrapping the functional tests from Rails. |
This is not the case, controller specs have a different underlying class requiring the |
So, the specific deprecated methods from rails 4 to rails 5 where If I'm not using these methods in my controller specs could I consider them ok? (asked in the spirit of deepening my understanding, not trying to be repetitive) |
If you have the |
Ok, I see your point about the need of improving the documentation. Thank you for your time and expertise! I'll follow your advice and move as many controller specs as I can into request specs |
We were having an internal discussion about this.
Looking at #1839 and considering the documentation has not been changed since it leaves me with questions.
Starting background jobs or sending notification mails is not client-facing behaviour. So that should not be covered in request specs. So far so good.
And here it's stating we should be using request specs for everything previously tested in conrollers. So I am wondering what the recommendation is for non client-facing behaviour. If controller tests are not to be used, and request specs should not test client-facing behavior, then where would you recommend testing them?
This is the kind of tests I currently use for controllers:
The text was updated successfully, but these errors were encountered: