What to test in Rails in controller/view/request/feature tests respectively? A best practice?

So I just started learning testing this week, when I scaffold something, Rails generates a lot of things in /spec folder. Some of them are clear, while the other remains gray to me even after Googling for two days. To name my confusions:

  1. Controllers: unit test or functional test? Or put in another word, does it get to touch real model or stubbed one?
  2. Controllers: do I test authentication or authorization paths, given that I unit tested policies (authorization rules)? Or should I just stub them out and only test normal cases (authenticated and authorized ones)?
  3. Views: do I test them? I’ve been told both do and do-not, what’s the benefit or necessity?
  4. Requests: is this where I put my integration tests? Do I need to test every corner case, coz it is a lot considering authentication and authorization even for simplest CRUD resources.
  5. Features: so I guess I only test some major usage paths here right? What’s the boundary between feature tests and integration test?

Dying to clear my head out now, really appreciate your options!

I’d highly recommend to look through the following. If you’re new to testing, i’d suggest to start with rails defaults: minitest and fixtures

http://sam-koblenski.blogspot.ca/2014/06/rspec-or-minitest-how-does-one-choose.html

MiniTest: Refactoring Test Unit and RSpec back to version 0.0.1 by Jared Ning MiniTest: Refactoring Test Unit and RSpec back to version 0.0.1 by Jared Ning - YouTube

https://whatdoitest.com/getting-friendly-with-fixtures

Also recommend these resources.

1 Like

These are all great resources. If I had to do it over and I was just getting into testing, the books I would read in this order are:

  1. Everyday Rails Testing with… by Aaron Sumner [PDF/iPad/Kindle] - It’s good for understanding the basics of RSpec and it’s syntax. It’s a great walkthrough of basic testing in Rails.
  2. Rails 4 Test Prescriptions by Rappin - This is a bit more higher level and less syntax-oriented and goes into principles/guidelines (e.g., what to test in a model, what makes great tests). I’m currently reading this and finding it to be a great read.
3 Likes

@ZombieHarvester @Jared_Smith @treble37 : thank you all for these great resources, I’ll definitely look into them.