Latest Tweet
- Dropped container of polynesian sauce on floor, fully-open side down. NOTHING CAME OUT. Chick-fil-A, champion of viscous sauces. 1 day ago
- More updates...
Categories
Tags
academia alcohol animals boyfriend cooking databases email forwards family Flickr food friends health Javascript Lexington Linux list Mario math movies music news OS X Perl PHP politics programming quizzes Rails rants reading Ruby screenshots shopping Sims sports themes tutorials Twilight is ridiculous vehicles video games videos weather Web development work zombies again-
Recent Comments
-
Random Quote
You don't volunteer for slugfests with vampires. It shortens your life expectancy.
— The Lunatic Cafe, Laurell K. Hamilton Syndication
All posts RSS feed
#post method in tests with a different controller
I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests. However, the #post method won’t allow you to set a different controller than the one in the @controller instance variable that’s defined in your test’s #setup. Well, by looking at how the #process method works, you can see that it just grabs the controller from @controller. Redefine that, and you’re good to go:
Ruby
@controller = LoginController.new
post(
:attempt_login,
{:user => {:name => ‘joe’, :password => ‘password’}}
)
@controller = old_controller
If you have several login methods, such as a #login_admin and #login_regular, you could make a wrapper to reduce duplication:
Ruby
old_controller = @controller
@controller = new_controller.new
yield
@controller = old_controller
end
def login_admin
wrap_with_controller do
post(
:attempt_login,
{:user => {:name => ‘root’, :password => ‘password’}}
)
end
end
def login_regular
wrap_with_controller do
post(
:attempt_login,
{:user => {:name => ‘joe’, :password => ‘password’}}
)
end
end
Cross-posted on my BigBold account.