Test for Control

I just got this thought when trying to write test for a controller yesterday: how exactly did you test a controller?

A controller is a route between model and view. It controls the system behavior given user action. But the validity of the data presented was already tested in the model, and whether the data shows right should be tested in the front end.To make things worse, our app at this point is just a single web page, without authentication, even. The controller literally just pass the data along from back to front. Now what?

After some searching, I found this guide about testing controller. It lists these things to test:

  • was the web request successful?
  • was the user redirected to the right page? (It’s a single page, where would you redirect anyway)
  • was the user successfully authenticated? (No authentication yet)
  • was the correct object stored in the response template?
  • was the appropriate message displayed to the user in the view? (Isn’t this front ends’ task?)

So now I just need to check two things, whether the request successful, and whether the object stored is the correct one. First, I test for the former. I made this spec:

describe HomeController, :type => :controller do
  describe 'GET #index' do
    it 'renders the :index view' do
      VCR.use_cassette('index') do
        get :index
        expect(response).to render_template("index")

and decided to run the test. It ran well, with 100% coverage. Nice.

Then I think again.

Simply testing to render the page gives me 100% coverage. Of course, it made sense. The code I wrote is short anyway, and it serves just a single task. It just, you know, seems too good to be true. And when something seems too good to be true, well, it usually is.

This, in turn, leads me to think deeper about testing and code coverage in general. Do a green test result and 100% code coverage guarantee perfect code? I mean, there’s nothing to stop you from writing assert (2+2) == 5 in your unit test. And I’m not talking about writing bad test intentionally to make your code pass. What if there is someone who truly think that 2+2 = 5? They will do that consistently both in the code and test, thus making their code “right”. But still, maybe they are indeed right, in the context if the program. No one can tell for sure whether that 2+2 = 5 is  just a typo, a bad joke, or an expected behavior.

hmm, maybe I should stop before this turn too much into a showerthought.


  • created a controller
  • learned in a guide how to test it
  • made a test
  • got 100% coverage
  • got existential crisis because of it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s