Created
September 2, 2010 16:10
-
-
Save lightscalar/562490 to your computer and use it in GitHub Desktop.
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
So, this helps a lot when testing applications using RSPEC that use the authlogic gem for authentication. | |
Just require and include the following module in your rspec file. Then you can simply write things like: | |
before(:each) do | |
login_as_user | |
end | |
at the top of your specs, and you should be good to go... | |
module ControllerHelpers | |
def login_as_user | |
# define user and session | |
@current_user = mock_model(User, :login => "test_user") | |
@current_session = mock_model(UserSession) | |
controller.stub!(:current_user).and_return @current_user | |
controller.stub!(:current_session).and_return @current_session | |
end | |
def not_logged_in | |
controller.stub!(:current_user).and_return nil | |
end | |
def current_user(stubs = {}) | |
@current_user ||= mock_model(User, stubs) | |
end | |
def user_session(stubs = {}, user_stubs = {}) | |
@current_user ||= mock_model(UserSession, {:user => current_user(user_stubs)}.merge(stubs)) | |
end | |
def login(session_stubs = {}, user_stubs = {}) | |
UserSession.stub!(:find).and_return(user_session(session_stubs, user_stubs)) | |
end | |
def logout | |
@user_session = nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment