2012-09-22 8 views

答えて

6

のアプローチをお勧めします。 コールバックコントローラを直接テストして、omniauth.authで渡された別の値にどのように反応するか、またはenv ["omniauth.auth"]が見つからないか正しくないかどうかを調べるとどうなりますか?次のリダイレクトは、あなたのシステムをテストしないomniauthプラグインをテストするのと同等です。

たとえば、これはテストでのものです(これはほんの数例に過ぎず、招待状、ユーザーなどのサインイン試行前のオムニハスハッシュとユーザー状態の他のバリエーションを検証するなど、管理者によって無効にされているアカウント):

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 

として定義stub_env_for_omniauth方法は次のとおりです。

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 
+0

は 'stub_env_for_omniauth'メソッドの戻りOmniAuth :: AuthHashオブジェクトではなく、ハッシュません万一? – user3021270

関連する問題