[rspec-users] Drawing the line between controller and integration tests

Mike Ossareh ossareh at twitch.tv
Mon Sep 10 23:52:20 UTC 2012


On Mon, Sep 10, 2012 at 2:51 PM, Fearless Fool <lists at ruby-forum.com> wrote:

> I'm trying to understand what belongs -- and what doesn't belong -- in
> controller tests and in integration tests.
>
> As a common example, assume I have a list of named widgets.  Somewhere
> in my code, I want to verify that
>
>     widget = FactoryGirl.create(:widget)
>     get :index
>
> generates a page that has the string #{widget.name} somewhere therein.
> But is that a controller test or an integration test?
>

Having spent a majority of my time writing test cases I've found there are
two main types, unit tests and integration tests. An integration test is
called such because it crosses a logical boundary (either in code or over a
socket, talking to a database, etc). Unit tests are usually self contained.

In your case you're talking about a "controller" test, if I try to fit that
into my mental model I can see it as either an integration test or a unit
test - and you just need to decide which you want to use. Usually I've
found integration tests to have the best bang for buck in terms of
stability and unit tests to be very very helpful in fixing bugs and
alleviating poor performance in chunks of code.

Assuming you had the following flow:

   1. User posts a form
   2. Controller creates a "widget"
   3. Controller redirects user to a list with "widget.name" in it.

You would be best served writing an integration test, imho. The fact it is
testing the controller and the model is neither here nor there. Let us
assume there are some functions that exist on Widget that are not directly
called by an HTTP action - then you'd want a unit test. That is the
following would be integration tests.

Widget
  #new
  #update_attributes

Where as the following may be unit tests:

Widget
  #friendly_name
  #synchronize_to_external_service




> As another example, assume a user must be logged on in order to access
> the widgets.  I can test authentication and authorization separately,
> but is it considered necessary to write an integration test for this?
> Or is this something you'd verify at the controller level?
>
> Etc.  I'm not looking for specific answers to the above as much as
> guiding principles, or at least pointers to same.  Thanks!
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120910/3f61c3c3/attachment-0001.html>


More information about the rspec-users mailing list