How to test a controller

I want to test the controllers of an existing project. How can i do it.

Actually when I use ruby script/spec rspec_scaffold modelname it
creates a spec controller for that model and a controller inside the app
folder.

So for testing an already existing project how should i proceed.

Thank you stephen.

Though it is easy to write spec for controller something stoped me like
I am proceeding the wrong way. Your words gives confidence. so let me
try and get back to you.

On Fri, Oct 17, 2008 at 1:35 AM, Mano ah [email protected] wrote:

I want to test the controllers of an existing project. How can i do it.

Actually when I use ruby script/spec rspec_scaffold modelname it
creates a spec controller for that model and a controller inside the app
folder.

The really obvious (and probably unhelpful) answer would be, “Just
write the specs, dude.”

Expanding on that: there’s nothing magic about the specs generated by
the scaffold. I’m not even sure that they’re great specs; they’re
just one opinion on how to test the default, plain-vanilla Rails
actions. They’ll stop passing the moment you change anything, and
then you’ll have to understand what they’re doing and fix them. (And
any non-trivial Rails app will end up doing something different
than what the default scaffold controllers do.)

I know in my case, I didn’t understand the controller specs at all
until I wrote some by hand. And then I wrote some more by hand, and
understood them better. For a while I was using make_resourceful and
mock_resourceful… I stopped, and rewrote those by hand, because
there was too much magic and I couldn’t follow what was going on. The
end result was more lines of code, but it was just as good as what the
plugins did and better-suited to my requirements, and I understood
it. That’s worth more than the specs themselves are worth.

So ignore the scaffold stuff, unless you want to look back at it as a
reference example, and write some specs for the way your controller
currently behaves. Do some mock objects for your models…or don’t,
as mocking too is just a matter of opinion. But test that the models
are at least being called to find the right objects. Write at least
one spec for the output of each controller action, making sure it
renders what you want it to, and then write more if you think you need
them.

Keep doing this, and keep maintaining your specs the same way you’d
refactor the actual code, and eventually you’ll start to Get It.
You’ll feel good about your specs without having to ask anyone.
Eventually you’ll look at the example code and go, “Man, I can do
better!” At that point, congratulations. You’ve cleared the level.
Proceed to Level 2 – whatever that may be for your particular style.
Just don’t stop learning things. If you stop, Donkey Kong keeps the
girl.

Hope this was helpful.


Have Fun,
Steve E. ([email protected])
ESCAPE POD - The Science Fiction Podcast Magazine
http://www.escapepod.org

Steph I sucessfully wrote my first test