Nelson Hsu
2007-Jun-23 19:25 UTC
How do I set up a test without using fixture in this case
Let''s say I have 3 records in SampleModel: id: 1 created_at: 5/3/2007 id: 2 created_at: 6/3/2007 id: 3 created_at: 7/3/2007 I have a controller action that should only return records that were created after 6/3/2007. I can see how to do it with fixtures, but can I perform this type of test with a mock object and/or stubs? Thanks! Nelson -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David Chelimsky
2007-Jun-23 20:23 UTC
Re: How do I set up a test without using fixture in this case
On 6/23/07, Nelson Hsu <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Let''s say I have 3 records in SampleModel: > > id: 1 > created_at: 5/3/2007 > > id: 2 > created_at: 6/3/2007 > > id: 3 > created_at: 7/3/2007 > > > I have a controller action that should only return records that were > created after 6/3/2007. I can see how to do it with fixtures, but can I > perform this type of test with a mock object and/or stubs?There''s really two parts to this - how would you do it using mocks, and what exactly would you mock? If it were me, it would look like this (using rspec, but you can do the same thing w/ test/unit+mocha or flexmock): describe SampleModelsController, "handling GET /date_filtered_action" do it "should ask SampleModel for items created before the submitted date" do SampleModel.should_receive(:find_created_before).with("6/22/2007") get "date_filtered_action", :cutoff => "6/22/2007" end ... end class SampleModelsController < Application def date_filtered_action @sampleModels = SampleModel.find_created_before(params[:cutoff]) ... end end And then I''d test the find_created_before method in my description of SampleModel''s behaviour (TestSampleModel if you''re using test/unit). Now if you want to use mocks in the model test as well, you could do this: describe SampleModel do it "should provide items created before a submitted date" do SampleModel.should_receive(:find).with(:conditions => ["created_at> ?", "6/22/2007"])SampleModel.find_created_before("2007-06-22") end end class SampleModel < ActiveRecord::Base def find_created_before(date) self.find(:all, :conditions => ["created_at > ?", Date.parse(date).to_s(:db))] end end Note that this sort of testing is very risky if you''re not also doing integration testing of some sort. For example, this would pass even if there was no created_at column in the sample_models table. I happen to do a lot of this sort of testing, but I do it in concert with customer acceptance tests (i.e. high level end-to-end tests). Hope that helps. David ps - I didn''t run this, so please forgive any errors. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---