On 9/15/07, David James <[EMAIL PROTECTED]> wrote: > I'm currently try to push my limits a little bit with some of my unit > testing -- trying to avoid saving ActiveRecord objects to the database and > take advantage of mock/stub objects. > > How far should I expect to get in this direction? From what I can tell, > ActiveRecord seems to fight me when it comes to associations. In other > words, many associations seem to require database queries. > > Have others had success in mocking/stubbing associations? In particular, > have you had luck with has_many :through? > > -David > > _______________________________________________ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
You can't assign mock objects to associations. You have to do stuff like @employee = Employee.new :name => "Pat" @mock_company = mock_model(Company, :name => "BigCo") @employee.stub!(:company).and_return @mock_company As far as has_many through, again you just mock out the association proxy. So if you've got class User < ActiveRecord::Base has_many :rentals has_many :videos, :through => :rentals end and you want to do some testing with videos, you'd have @videos_proxy = mock("videos proxy") @user.stub!(:videos).and_return @videos_proxy You don't deal with the fact that rentals plays in anywhere, at least if you're trying to isolate from the database. Pat _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users