8

My Rails 3.2アプリはOmniAuthとDeviseを使ってTwitterでサインインします。認証システムは正常に動作します。 rspecに統合テストを書いて、すべてが正しく動作することを確認したいと思います。 wikiの情報を使って、私は次のように書いていますが、私は物事を逃していることを知っています。設定/環境でのtest.rbの下Rails rspecとomniauth(統合テスト)

、私は次の行を持って

OmniAuth.config.test_mode = true 
OmniAuth.config.mock_auth[:twitter] = {:provider => 'twitter', :uid => '123545'} 

私のRSpecのテストでは、次のようになります。

describe "Authentications" do 
    context "without signing into app" do 

    it "twitter sign in button should lead to twitter authentication page" do 
     visit root_path 
     click_link "Sign in with Twitter" 
     Authentication.last.uid.should == '123545' 
    end 

    end 
end 

認証は、私のモデルの名前であると.UID呼び出しますin rails consoleは文字列を返します。

私はこのテストを実行すると、私は次のエラーを取得しています:

Failure/Error: Authentication.last.uid.should == '123545' 
NoMethodError: 
undefined method `uid' for nil:NilClass 

誰も私が提供されOmniAuthのモックを使用する方法を見つけ出す手助けすることはできますか? についての理由は、なぜですか?と同様に動作します。

答えて

14

before do 
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter] 
end 

あなたはこのリンク上でより多くの情報を見つけることができます。

記号キーを使用してから私のモックオブジェクトを変更した後:

OmniAuth.config.mock_auth[:twitter] = { 
    :uid => '1337', 
    :provider => 'twitter', 
    :info => { 
     :name => 'JonnieHallman' 
    } 
    } 

文字列キーを使用する:

OmniAuth.config.mock_auth[:twitter] = { 
    'uid' => '1337', 
    'provider' => 'twitter', 
    'info' => { 
     'name' => 'JonnieHallman' 
    } 
    } 

それが働きました。

そして、あなたはあなたのテストケースで

request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter] 

どこがありますか?

+1

こんにちは!私はrequest.env ["omniauth.auth"]を試しましたが、 "nilクラス用の未定義メソッドenv"が表示されます – izumeroot

+0

ここに正しい解決策がありますhttps://gist.github.com/kinopyo/1338738 – izumeroot

6

これらの2行をspec_helper.rbに移動してみましたか?

OmniAuth.config.test_mode = true 
OmniAuth.config.mock_auth[:twitter] = {:provider => 'twitter', :uid => '123545'} 

はまた、あなたのテストファイル内のブロックの前に以下を追加します。私は似たように実行https://github.com/intridea/omniauth/wiki/Integration-Testing

1

選択した解決策は私のためには機能しません。 私のソリューションは、私がここにhttps://gist.github.com/kinopyo/1338738 と公式ドキュメントhttps://github.com/intridea/omniauth/wiki/Integration-Testing から入手:

# in spec/support/omniauth_macros.rb 
module OmniauthMacros 
    def mock_auth_hash 
    # The mock_auth configuration allows you to set per-provider (or default) 
    # authentication hashes to return during integration testing. 
    OmniAuth.config.mock_auth[:odnoklassniki] = OmniAuth::AuthHash.new({ 
     :provider => 'odnoklassniki', 
     :uid => '123545', 
     :info => OmniAuth::AuthHash::InfoHash.new({ 
      :name => 'mockuser' 
     }) 
    }) 

    end 
end 

# in spec/spec_helper.rb 
RSpec.configure do |config| 

    # email spec 
    config.include(EmailSpec::Helpers) 
    config.include(EmailSpec::Matchers) 
end 
OmniAuth.config.test_mode = true 

# in spec example: 
visit new_user_registration_path 
mock_auth_hash 
find('#btn-odnoklassniki').click # here is link generated as omniauth_authorize_path(resource_name, provider) 
+0

ありがとうございますAuthHash.newの方へ(実際のインテグレーションテストWebページにありますが)私は、本当の秘密とトークンを含む私の認証モデルのFactoryGirlを持っていれば、これをkoalaと連携させることができました(例えば有効なfacebookログインからコピー&ペーストトランジション) – Ben

1

私は非常に短いでthis answer

を示唆して...[ 'UID']セッションをテストIは、が(設定された:例えば、コールバック

に取り付けられているどのようなコード要求

  • テストを作成が
  • モックを設定

    • ユーザーが見ているものだけをテストするために選ぶ、あるいは、むしろ、見ていない)

      私のコード...

      設定/環境/ test.rb

      Rails.application.configure do 
          ... 
          OmniAuth.config.test_mode = true 
          OmniAuth.config.mock_auth[:linkedin] = { 
           'provider' => 'linkedin', 
           'uid' => '123545', 
           'info'=> 
           { 'email'=>'[email protected]', 
           'first_name'=>'Dave', 
           'last_name'=>'Wallace' } 
          } 
      end 
      

      スペック/機能/ sign_in_feature_spec.rb

      require 'rails_helper' 
      
      feature 'Sign in with LinkedIn' do 
      
          before do 
          OmniAuth.config.add_mock(:linkedin, {:uid => '12345'}) 
          end 
      
          let(:user) { create(:user) } 
      
          scenario 'with valid email and password' do 
          visit '/' 
          expect(page).to have_no_content 'Sign Out' 
          click_link 'nav-sign-in' # image/button: Sign in with LinkedIn 
          expect(page).to have_content 'Sign Out' 
          end 
      end  
      

      私は、このソリューション改善する可能性がある場合/方法を知ってみましょう(と私のコードを!)

    +0

    素晴らしい!解決策が問題を解決することを確認します – jedi

    関連する問題