2011-09-01 16 views
11

Ho seguito il cast rails di omniauth per creare l'autenticazione per twitter (http://railscasts.com/episodes/235-omniauth-part-1?view=comments). Funziona bene nello sviluppo ma non riesco a ottenere rspec per rilevare che ho creato l'autenticazione. Ecco il mio frammento per creare funzione nel mio controller di autenticazione:Omniauth Rspec testing problem

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 

inclusi nei miei percorsi:

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

Ho la seguente configurazione nel mio 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"} }) 

Qui è la mia codice rspec che non funziona:

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 

errore ottengo è:

1) AuthenticationsController Post 'creare' successo dovrebbe creare autenticazione Fallimento/Errore: lambda contano dovrebbe essere stato modificato da 1, ma è stato cambiato da 0 # ./spec/controllers /authentications_controller_spec.rb:57

Ho controllato tutto e non riesco a capire cosa sto facendo male. Qualcuno può aiutare?

risposta

3

ho finalmente capito cosa c'era che non andava. in my mock: auth_token è supposto di essere: token. Ciò stava causando la convalida fallita.

0

Non dovrebbe essere una richiesta di ottenere?

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

quasi sicuro è una richiesta di posta. inoltre ho provato ad ottenere e non funziona neanche. – Whereisccguys