Hi Craig A link to a gist with the test code would certainly help here - but I would say you should be able to test observers by themselves easily enough, not needing any external dependencies.
One thing to note is that more and more, I'm considering ARel calls (chained or otherwise) from outside a model a code smell - and the same goes for find_by_*/find_all_by_* methods. Create scopes or class methods (that use ARel, etc under the hood) and then you've got clear single-method interfaces into the model for the records you need. Cheers -- Pat On 08/06/2012, at 4:49 AM, Craig Read wrote: > Hi Everyone, > > I have an observer which performs some calculations based on a number of > relationships (including polymorphic relationships) and stores (aka caches) > the results on a model related to the one being observed. > > I ran into a bit of yak shaving while getting that observer adequately tested > (via rspec). > I started off using stubs, but finally ran into an issue where a calculation > was using a count of related rows using a "where" to filter the relations. > The test barfed because the array of stubs I was (quite happily) using for my > other rspec tests wouldn't respond to where. > > I tried for hours to fix this and got nowhere, so I ended up changing all my > spec helpers so they instantiated real models instead of stubs. > The tests now pass, I refactored my test code to be a little cleaner, and the > tests actually run a little faster (that bit has me stumped). > But I still feel dirty having those tests hit the database. > > I have the rspec book, which covers testing models, controllers and views, > but says nothing about testing observers. > > How do you guys suggest testing observers via rspec? > Or should I test the impacts my observers have via cucumber? > > Cheers, > > -- > Craig Read > > @Catharz > https://github.com/Catharz > http://stackoverflow.com/users/158893/catharz > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby or Rails Oceania" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rails-oceania?hl=en. -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
