> If it’s that black-and-white that Request specs are to be preferred over > Controller Specs, you should consider reflecting that in the documentation on > RSpec Controller Specs. Right now, Controller Specs are documented very much > the way they would have been before this debate appeared (and certainly > before the moves were made by Rails to move away from controller tests). In > fact, two of the four examples in the introduction to Controller Specs are > about rendered templates and controller instance variables. (BTW, the Request > Spec overview also mentions #render_template.)
Thats because the documentation was likely written when controller specs where the only option, and we as a team haven't gotten around to updating them, nor am I likely to any time soon, but I'd happily review contributions on this front. > What I have done with Controller Specs is to focus on making sure that I test > every piece of code that is in the Controller to ensure that they do what is > expected of them, and the existence of robust controller specs has allowed me > to discover and resolve a number of breaking changes All of which would have been apparent with request specs too. > While a request spec will likely find a problem in your program’s behavior > (if you’ve figured out enough of the logic to drive all the different paths > through the code, which is not trivial when you’re focusing on the big > picture of the request), A controller always executes in the context of the request, a controller spec just skips parts of the stack, but not all of the stack, and can actually introduce bugs where you pass in things which are not actually how rack parses them. > In Controller tests, I stub out almost everything not in the controller, so > that it really is like a controller unit test. You can also stub out like that in a request spec, however thats not what I mean by "controller specs are integration tests" a controller spec always has part of the rack stack wrapped around it, and you don't er, control, the initialisation of the controller. So it is never a unit test, you are always integrating parts of Rails with your controller. I actually wrote proper controller unit tests as part of an experimental setup, with an eye to improving controller specs in rspec-rails, however its really difficult and involved a large knowledge of rails setup, thus its far better to give in and accept the truth, that they are integration tests, and treat them as such. You can still mock out your service layer in that context, (or go down the DI route to allow control of your inner stack) but the Rails part is so coupled to the router and rack stack you simply have to accept it (or, I guess PRs welcome 😂) Hope that helps Jon -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/f1320e23-d12c-4e47-90ea-d80f6f598077%40www.fastmail.com.