Polecam podejście alternatywne, prostsze podejście. Co, jeśli przetestowałeś kontroler oddzwonienia bezpośrednio, aby zobaczyć, jak reaguje na różne wartości przekazane do niego w omniauth.auth lub jeśli env ["omniauth.auth"] brakuje lub jest niepoprawny. Następujące przekierowania będą równoważne testowaniu wtyczki omniauth, która nie testuje systemu.
Na przykład, tutaj jest to, co mamy w naszych testach (to tylko kilka przykładów, mamy wiele innych, które zweryfikować inne odmiany omniauth hash i stanu użytkownika uprzedniej próbie logowania, takie jak stan zaproszenia, użytkownik konto jest zablokowane przez administratorów, etc.):
describe Users::OmniauthCallbacksController do
before :each do
# This a Devise specific thing for functional tests. See https://github.com/plataformatec/devise/issues/608
request.env["devise.mapping"] = Devise.mappings[:user]
end
describe ".create" do
it "should redirect back to sign_up page with an error when omniauth.auth is missing" do
@controller.stub!(:env).and_return({"some_other_key" => "some_other_value"})
get :facebook
flash[:error].should be
flash[:error].should match /Unexpected response from Facebook\./
response.should redirect_to new_user_registration_url
end
it "should redirect back to sign_up page with an error when provider is missing" do
stub_env_for_omniauth(nil)
get :facebook
flash[:error].should be
flash[:error].should match /Unexpected response from Facebook: Provider information is missing/
response.should redirect_to new_user_registration_url
end
end
end
z stub_env_for_omniauth
metody zdefiniowane następująco:
def stub_env_for_omniauth(provider = "facebook", uid = "1234567", email = "[email protected]", name = "John Doe")
env = { "omniauth.auth" => { "provider" => provider, "uid" => uid, "info" => { "email" => email, "name" => name } } }
@controller.stub!(:env).and_return(env)
env
end
nie powinno się 'metoda stub_env_for_omniauth' powrotną OmniAuth :: AuthHash obiekt, zamiast mieszania ? – user3021270