How do I mock an OmniAuth hash using Rails and minitest?

前端 未结 1 1459
遇见更好的自我
遇见更好的自我 2021-01-14 05:37

I\'m using Rails 5 with minitest. I want to mock logging into my sessions controller, which relies on omniauth (I use Google and FB for login). I have this in my controlle

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-14 05:49

    The OmniAuth docs state

    When you try to test the OmniAuth, you need to set two env variables

    and provide examples using RSpec

    before do
      Rails.application.env_config["devise.mapping"] = Devise.mappings[:user] # If using Devise
      Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
    end
    

    In your case, it seems like you may need to set

    Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
    

    in your setup_omniauth_mock method, after the call to OmniAuth.config.add_mock.

    0 讨论(0)
提交回复
热议问题