2011-09-01 12 views
11

Seguí el molde de rieles de omniauth para crear autenticación para twitter (http://railscasts.com/episodes/235-omniauth-part-1?view=comments). Funciona bien en el desarrollo pero no puedo obtener rspec para detectar que he creado la autenticación. Aquí está mi fragmento para crear la función en mi controlador de autenticación:Problema de prueba Omniauth Rspec

def create 
    begin 

    auth_hash = request.env["omniauth.auth"] 
    @auth = current_user.authentications.build(:provider  => auth_hash['provider'], 
               :uid   => auth_hash['uid'], 
               :name   => auth_hash['user_info']['name'], 
               :nickname  => auth_hash['user_info']['nickname'], 
               :image  => auth_hash['user_info']['image'] 
              ) 

    if @auth.provider.downcase == "twitter" 
     @auth.auth_token  = auth_hash['credentials']['token'] 
     @auth.secret_token = auth_hash['credentials']['secret'] 
     @auth.site   = auth_hash['user_info']['urls']['Twitter'] 
    elsif @auth.provider == "Facebook" 

    end 

    rescue 
    redirect_to current_user, :flash => { :error => "Missing oauth data!! Check with administrator!"} 
    else 
    if @auth.save 
     msg = "Authentication success" 
    else 
     msg = "Already have authentication" 
    end 
    redirect_to current_user, :notice => msg 
    end 
end 

incluido en mis rutas:

match '/auth/:provider/callback' => 'authentications#create' 

Tengo la siguiente configuración en mi rspec_helper:

OmniAuth.config.test_mode = true 
OmniAuth.config.add_mock(:twitter, { :provider => "twitter", 
            :uid   => "1234", 
            :user_info => { :name  => "Bob hope", 
                 :nickname => "bobby", 
                 :urls  => {:Twitter => "www.twitter.com/bobster"}}, 
            :credentials => { :auth_token => "lk2j3lkjasldkjflk3ljsdf"} }) 

Aquí está mi código rspec que no está funcionando:

describe "Post 'create'" do 
    before(:each) do 
     @user = Factory(:user) 
     sign_in @user 
    end 

    describe "success" do 
     before(:each) do 
     request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter] 
     end 


     it "should create authentication" do 
     lambda do 
      post :create, :provider => "twitter" 
      response.should redirect_to(@user) 
     end.should change(Authentication, :count).by(1) 
     end 
    end 
    end 

error que consigo es:

1) AuthenticationsController Post 'crear' éxito debe crear la autenticación Fallo/Error: lambda no recuento debería haber sido cambiado por 1, pero fue cambiado por 0 # ./spec/controllers /authentications_controller_spec.rb:57

He comprobado todo y no puedo entender lo que estoy haciendo mal. ¿Alguien puede ayudar?

Respuesta

3

finalmente me di cuenta de lo que estaba mal. en mi simulacro: auth_token se supone que es: token. Eso estaba causando la validación fallida.

0

¿No debería ser una solicitud de obtención?

describe "success" do 
    before(:each) do 
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter] 
    end 


    it "should create authentication" do 
    lambda do 
     get :create, :provider => "twitter" 
     response.should redirect_to(@user) 
    end.should change(Authentication, :count).by(1) 
    end 
end 
+1

bastante seguro es una solicitud posterior. Además lo intenté y tampoco funciona. – Whereisccguys