I''m a newbie to rails, but fairly experienced with unit-testing. While I think it is great to have a layer of "integration tests", which is what the functional tests in rails appear to be, I''m curious why things are not (more) set up to ease unit test of controller and views? David
Because you shouldn''t have any code worth testing in your controllers. Controllers are purly glue code which requests stuff from the data layer and hands it to the presentation layer in convienient chunks. If you find yourself writing any code in controllers which would warrant unit testing but which isn''t exposed to the presentation layer than chances are this code should be in a related model. The classic example for this is that a fulltext seach should be accessible by doing Articles.search(query) instead of coded into a search action. That being said, unit testing the effects of controllers ( whats being rendered, how many mails were sent etc) should be trivial with the current setup. On 5/1/05, David Corbin <dcorbin-wmGZ+vDKSyrZJqsBc5GL+g@public.gmane.org> wrote:> I''m a newbie to rails, but fairly experienced with unit-testing. While I > think it is great to have a layer of "integration tests", which is what the > functional tests in rails appear to be, I''m curious why things are not (more) > set up to ease unit test of controller and views? > > David > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
On Sunday 01 May 2005 02:52 pm, Tobias Luetke wrote:> Because you shouldn''t have any code worth testing in your controllers. > Controllers are purly glue code which requests stuff from the data > layer and hands it to the presentation layer in convienient chunks. If > you find yourself writing any code in controllers which would warrant > unit testing but which isn''t exposed to the presentation layer than > chances are this code should be in a related model.Maybe we have a different view of what code should be unit tested. If I had to make up a guidleine off the cuff, I''d say any code that has two paths of execution probably needs to have a unit-test. each, while, unless, if are all indications of this. In Ruby, I''m tempted to do even more unit-testing because of the dynamic nature of the language. Now, with Rails, I haven''t played enough to understand how often that occurrs in controllers, but it certainly is going to happen in views.> The classic example for this is that a fulltext seach should be > accessible by doing Articles.search(query) instead of coded into a > search action.Agreed.> That being said, unit testing the effects of controllers ( whats being > rendered, how many mails were sent etc) should be trivial with the > current setup.For me, it''s about testing all the interactions with request/response/session etc. And from my limited effort, it''s not that hard (mostly I was just curious). There are things I might do differently in rails to make it easier, but Ruby is so good I can overcome most of those things without being too nasty.> > On 5/1/05, David Corbin <dcorbin-wmGZ+vDKSyrZJqsBc5GL+g@public.gmane.org> wrote: > > I''m a newbie to rails, but fairly experienced with unit-testing. While I > > think it is great to have a layer of "integration tests", which is what > > the functional tests in rails appear to be, I''m curious why things are > > not (more) set up to ease unit test of controller and views? > > > > David > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails
This may be a case of terminology here - Rails is encouraging the traditional view of what a unit test is - that it should test a single class (for example a model). Normally you''ll take something you want to unit test, and mock/stub out any collaborators so that any tests only test the unit in questions. The controller classes are tied to the wider rails framework in a way that makes it non-trivial to mock or stub out all collaborators. Even if it was easy to do, I''m not sure it''s that valid - you still want to test at a functional level, and the controllers are an obvious place to do this. sam On 5/2/05, David Corbin <dcorbin-wmGZ+vDKSyrZJqsBc5GL+g@public.gmane.org> wrote:> On Sunday 01 May 2005 02:52 pm, Tobias Luetke wrote: > > Because you shouldn''t have any code worth testing in your controllers. > > Controllers are purly glue code which requests stuff from the data > > layer and hands it to the presentation layer in convienient chunks. If > > you find yourself writing any code in controllers which would warrant > > unit testing but which isn''t exposed to the presentation layer than > > chances are this code should be in a related model. > > Maybe we have a different view of what code should be unit tested. If I had > to make up a guidleine off the cuff, I''d say any code that has two paths of > execution probably needs to have a unit-test. each, while, unless, if are all > indications of this. In Ruby, I''m tempted to do even more unit-testing > because of the dynamic nature of the language. > > Now, with Rails, I haven''t played enough to understand how often that occurrs > in controllers, but it certainly is going to happen in views. > > > The classic example for this is that a fulltext seach should be > > accessible by doing Articles.search(query) instead of coded into a > > search action. > > Agreed. > > > That being said, unit testing the effects of controllers ( whats being > > rendered, how many mails were sent etc) should be trivial with the > > current setup. > > For me, it''s about testing all the interactions with request/response/session > etc. > > And from my limited effort, it''s not that hard (mostly I was just curious). > There are things I might do differently in rails to make it easier, but Ruby > is so good I can overcome most of those things without being too nasty. > > > > > On 5/1/05, David Corbin <dcorbin-wmGZ+vDKSyrZJqsBc5GL+g@public.gmane.org> wrote: > > > I''m a newbie to rails, but fairly experienced with unit-testing. While I > > > think it is great to have a layer of "integration tests", which is what > > > the functional tests in rails appear to be, I''m curious why things are > > > not (more) set up to ease unit test of controller and views? > > > > > > David > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/