Forum: RSpec How to test a controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-10-17 07:35
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.
994e42bda994be2cd1d791f18ee6d561?d=identicon&s=25 Stephen Eley (Guest)
on 2008-10-17 08:29
(Received via mailing list)
On Fri, Oct 17, 2008 at 1:35 AM, Mano ah <lists@ruby-forum.com> 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 Eley (sfeley@gmail.com)
   ESCAPE POD - The Science Fiction Podcast Magazine
   http://www.escapepod.org
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-10-17 08:57
 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.
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-10-17 09:26
Steph I sucessfully wrote my first test
This topic is locked and can not be replied to.