Observations on writing a controller spec

Date: 2015/10/12

Write a controller spec.

Why is this exercise difficult for inexperienced testers?
As an inexperienced tester I can give my thoughts.

I started out trying to use doubles right off the bat instead of going the easier route of using the model given.
That was my first mistake, trying to go too fast.

As I was using the doubles I was realizing that I was getting frustrated because I didn’t know how to use them.
Rails was continuously probing the double for methods that I didn’t assign. I would think that I covered everyting I needed and rails needed a model_name…
Getting to the point where I was testing my knowledge of doubles, reading documentation and googling and grasping at straws just to get it to work
and not getting anywhere near my initial objective of testing the controller at hand… I decided to go back to testing without doubles.

Going back it was much easier.
Simple. Right?

post :create, person: { first_name: 'test' }

But even here I was going too fast, thinking that everything is as forgiving as Ruby.
Everything is not as forgiving as Ruby.
Ruby is purposly written to be forgiving and I think we aspiring devs need to remember that.

For example, I was thinking that I could just write
post :create, person (rails should be able to see that I am passing it a person object and that it’s first_name is nil, why do I need to do person: { first_name: 'test'})
expect(response).to redirect_to people_path(person) (me expecting the rspec redirect_to method to know how to use routing helpers the way rails uses it in the controllers)

After receiving errors I wasn’t expecting, I started focusing on why it wasn’t being forgiving and thought something was wrong with IT.
Rails is asking too much or why doesn’t rspec know about how rails does things… LOL

It took some stepping back and patience and reflection and a few hours to conclude that I was expecting too much help from rails and rspec.
And I was arrogant thinking that this couldn’t be so hard to complete.

Slow down, take your time and read the documentation and examples.
Like it was 20 years ago back in college (for me).

So I think my lesson here is to be a student.
Write notes.
Don’t be arrogant or think because you have many years of experience that this will come easy, it’s not easy until you put in the work of learning and practicing for hundreds of hours.
Don’t get frustrated or lose focus. To be here you have to be intelligent and with time and dedication to your craft, you will get it.
Be patient.


Really appreciate you writing this up!

We get a lot of questions about this exercise. I’m hoping to revamp it soon.