I found that by mocking Warden itself I was able to achieve leaner tests that did not require actually creating an instance of the user i was faking, nor did it require calling several helper methods.
Below is the snippet(gist) we now use in our application to fake authentication in our controller specs. I hope you find it useful.
module AuthenticationHelpers # Example usage: login mock_user(Editor) def login(user) request.env['warden'] = mock(Warden, :authenticate => user, :authenticate! => user) end def mock_user(type, stubs={}) mock_model(type, stubs).as_null_object end end
Thanks. Exactly what I needed to test authentication with Devise.
ReplyDeleteIf you are looking to fake authentication in your features without going to the sign in page, check out the Warden docs: https://github.com/plataformatec/devise/wiki/How-To%3a-Test-with-Capybara
ReplyDeleteSimply place your chips on the designated positions, spin the wheel, and hope for a winning number. The roulette wheel consists of a stable picket disk slightly convex in bet365 shape. Around its rim are metal partitions known as as|often identified as} separators or frets, and the compartments or pockets between these are called canoes by roulette croupiers. Thirty-six of these compartments, painted alternately pink and black, are numbered nonconsecutively from 1 to 36.
ReplyDelete