Friday, February 24, 2006

Calling methods on another Controller in Functional Tests

Today while trying to write a functional test in our rails app we came across an issue wherein the session had to be setup in a particular manner before we could actually test this action we were interested in.

post :delete_flower

so we wanted delete_flower action to be called on the FlowerController. But for the test to pass we needed some action on GardenController to be called (so as to mimic what would happen on the GUI)

but get and post methods are called only on the current controller. So we wrote up this helper method in the test_helper.rb file

class Test::Unit::TestCase
def switch_controller(klass)
old_controller = @controller
@controller = klass.new
yield
@controller = old_controller
end
end

So in our code we could do something like

def test_delete
switch_controller(GardenController) do
post :plant_flower
end
post :delete_flower
assert_no_flower_in_garden
end


[Updated later] - Just saw that they have added the concept of an Integration Test for the next release of rails.
So Looks like all this might not be necessary !!

0 Comments:

Post a Comment

<< Home