[#16775] Problem with stubbing find on AR associations
-
James Mead
Date: 2008-01-02 19:53 Sender: Jonathan Leighton
This is the output from running the test:
$ ruby test/acceptance/rails_has_many_find_acceptance_test.rb Loaded suite test/acceptance/rails_has_many_find_acceptance_test Started F.. Finished in 0.032379 seconds. 1) Failure: test_find_stubbed_on_the_association_should_work(RailsHasManyFind AcceptanceTest) [/home/turnip/Projects/mocha/test/test_runner.rb:24:in `assert_passed' test/acceptance/rails_has_many_find_acceptance_test.rb:29:in `test_find_stubbed_on_the_association_should_work' /home/turnip/Projects/mocha/lib/mocha/test_case_adapter.rb:19:in `__send__' /home/turnip/Projects/mocha/lib/mocha/test_case_adapter.rb:19:in `run']: Test failed unexpectedly with message: Error: test_me(): ActiveRecord::RecordNotFound: Couldn't find Room with ID=6 AND (rooms.house_id = NULL) /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1267:in `find_one' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1250:in `find_from_ids' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:504:in `find' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/has_many_association.rb:66:in `find' test/acceptance/rails_has_many_find_acceptance_test.rb:27:in `test_me' /home/turnip/Projects/mocha/lib/mocha/test_case_adapter.rb:19:in `__send__' /home/turnip/Projects/mocha/lib/mocha/test_case_adapter.rb:19:in `run' /home/turnip/Projects/mocha/test/test_runner.rb:15:in `run_test' test/acceptance/rails_has_many_find_acceptance_test.rb:24:in `test_find_stubbed_on_the_association_should_work' /home/turnip/Projects/mocha/lib/mocha/test_case_adapter.rb:19:in `__send__' /home/turnip/Projects/mocha/lib/mocha/test_case_adapter.rb:19:in `run'. 3 tests, 1 assertions, 1 failures, 0 errorsOf course
ActiveRecord::Base#findshould actually be called because the method is stubbed out... -
James Mead
Date: 2008-01-02 23:24 Sender: James Mead
I haven't yet had a chance to run the tests you attached, but I think there have always been problems trying to stub directly on the
ActiveRecordassociation proxies - they aren't normal objects. The simplest solution is to stub the proxy method generated by the association - in this caseHouse#roomsto replace the proxy object itself.Using the recently added block initializer code in Mocha, your first test could be something like this...
def test_find_stubbed_on_the_association_should_work test_result = run_test do house, room = House.new, Room.new rooms = stub { stubs(:find).with(6).returns(room) } house.stubs(:rooms).returns(rooms) assert_equal room, house.rooms.find(6) end assert_passed(test_result) endAgain, I'm sorry but I haven't tested this thoroughly. Let me know if this doesn't solve your problem.
Cheers, James.
-
James Mead
Date: 2008-01-02 23:38 Sender: Jonathan Leighton
Hi James,
Thanks for your speedy response. I figured it was probably something to do with AR's magic getting in the way. Stubbing the association proxy definitely does solve the problem (one of the tests I wrote shows this) but it would be lovely if that wasn't necessary :)
Cheers, Jon
-
James Mead
Date: 2008-01-03 09:23 Sender: James Mead
Hi Jonathan,
My response was obviously a bit too speedy - I'm afraid I'd only looked at the failing test. BTW I really appreciate you taking the time to create failing tests for the bug report - they really help communication - especially if I were to read them properly!
Anyway, now that I understand the problem a bit better, I'll have a bit more of a think. One reason I haven't done anything about this previously is that there is a workaround which isn't too ugly and I'd like to avoid any Rails-specific code creeping into Mocha. If it's not possible to come up with a generic solution, it might be worth splitting the Mocha Rails plugin out into a separate project and adding the Rails-specific code in that.
Cheers, James.
-
James Mead
Date: 2008-01-03 10:10 Sender: Jonathan Leighton
Hi James,
No problem, thanks for taking the time to look at it.
Jon
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
