[mocha-developer] Login systems : stubbing accounts and AR association proxies

Jonathan del Strother maillist at steelskies.com
Tue Feb 6 08:19:02 EST 2007


My Rails site has a fair amount of login and 'ownership' logic.  For  
instance, we have a number of clients (companies), each of which has  
several accounts.  A client owns a number of different types of  
resources, and shouldn't see any other clients' resources, so, for  
example, our ScenesController contains a lot of references to   
'current_client.scenes', rather than Scene.find(:all)


All of which works pretty well, but it can make testing very  
tedious.  I always need to set up a fake client, and a fake account,  
and log in with that account, and make sure that whatever resource  
I'm dealing with in that unit test is assigned to the fake client,  
and so on.
I'm starting to wonder whether I ought to stub all the login and  
ownership logic out of my functional tests, so I can concentrate on  
whatever resource I'm dealing with in that given test.  All the login/ 
ownership stuff could be handled in integration testing, which is  
probably better suited to it anyway, due to having real sessions etc  
etc.

I'm having difficulty doing so, however.  The controller uses some  
current_account / current_client methods to find whoever's logged in  
- stubbing those to return a fake client/account is easy, but you're  
then left with problems with Rails' association proxies.  There's  
lots of use of things like  current_client.scenes.build, which are  
not so easy to stub out (particularly as, AFAICT, you can't make use  
of any arguments passed into a stubbed method).


Has anyone else done anything similar, or am I just creating more  
work for myself by trying to remove login details from functional  
testing?


Jon


More information about the mocha-developer mailing list