After I've changed sign in logic, to use only facebook authorization my helper to sign in user in tests crashes. I used omniauth-facebook gem to handle authorization and clearance as legacy gem.
I've tried many times handle that problem.
I've tried many times handle that problem.
- Easiest way is click and fill, but that means you need connect to fb in test. We can handle that with VCR, which will mock your fb requests. Unlucky on every test we have different user id, so recorded request will no match.
- Mock everything !!! Yeah, I thought I'll mock essential methods in application controller, current_user, signed_in? etc. In rspec we use rspec-mock, which has similar api as mocha.
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
#spec/support/prefork/request_helpers.rb module RequestHelpers def sign_in controllers_to_mock.each do |controller| controller.any_instance.stub(:current_user).and_return(user) controller.any_instance.stub(:signed_in?).and_return(true) end ensure_signed_in end def sign_out controllers_to_mock.each do |controller| controller.any_instance.unstub(:current_user) controller.any_instance.unstub(:signed_in?) end end private def controllers_to_mock ApplicationController.subclasses[3..-1] end end - The final solution was already designed in gem omiauth:
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
#spec/support/prefork/request_helpers.rb module RequestHelpers def sign_in_as(user, password = "password") OmniAuth.config.add_mock(:facebook, {"uid" => user.facebook_uid}) visit root_path click_link "Sign in" ensure_signed_in end def ensure_signed_in visit root_path wait_until {page.should have_content "Sign out"} end end
Brak komentarzy:
Prześlij komentarz