On Oct 23, 2010, at 11:04 AM, deploy wrote: > I've been spec'ing for a long time, but I've never discovered the > answer to this, so a definitive answer would be great! > > Whats the best way to spec a method which basically filters an > association? > > class Thing < ActiveRecord::Base > has_many :others > > def foos > others.all(:conditions => { ... }) > end > end > > I don't like the idea of creating a load of associated records in my > spec and testing the returned result as I am not testing the > associated model and as such would prefer any references to it to be > mocked out, so the best I can some up with is this super lame test > which is clearly way too implementation specific and essentially > useless. > > describe Thing do > describe 'foos' do > before :each do > @thing = Thing.new > @others_proxy = mock('others proxy') > @others_proxy.stub!(:all) > @thing.stub!(:others).and_return(@others_proxy) > end > > it 'should return the associated others which meet the conditions' > do > @others_proxy.should_receive(:all).with({ :conditions => ' ... > ' }) > @thing.foos > end > end > end > > So what's the best practice in this case?
<tangent> There are no 'best practices' absent _all_ of the context of the application, dev stack, dev team, other apps the team builds, etc. That aside ... </tangent> When it comes to spec'ing filters, I tend to just use ActiveRecord (or whatever ORM I'm using). It's OK for some examples to hit the DB, and this is a good case for allowing it in my view, as the examples end up being more clear and readable and less brittle. HTH, David > Pastie is here for pretty formatting: http://pastie.org/1242892 > > Thanks. > > P.S. This is old code I've inherited so I'm just trying to retrofit > RSpec 1.3x to it before I start refactoring and bringing up to date > with rails 3/rspec 2, so basically what I mean is what's the answer if > the model simply is how it is and can't be changed? _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users