Created
September 2, 2013 23:57
-
-
Save jeremywrowe/6418318 to your computer and use it in GitHub Desktop.
Getting current user in tests is slow and painful.. but you can do something like this to speed up your test suite. Writing a blog about it sometime in the future.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bypass_current_user(user, &block) | |
raise "Block must be given.." unless block_given? | |
$current_user = user | |
begin | |
ApplicationController.class_exec do | |
alias original_current_user current_user | |
def current_user | |
$current_user | |
end | |
yield | |
end | |
ensure | |
ApplicationController.class_exec do | |
if self.instance_methods.include?(:original_current_user) | |
$current_user = nil | |
alias current_user original_current_user | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
test "do something fancy" do | |
bypass_current_user(@user) do | |
get_via_redirect '/' | |
assert_equal '/', path | |
end | |
end | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment