8

मेरा रेल 3.2 ऐप ट्विटर के साथ साइन इन करने के लिए ओमनीएथ और डेविस का उपयोग करता है। प्रमाणीकरण प्रणाली ठीक काम करता है। मैं यह सुनिश्चित करने के लिए कि सब कुछ काम करता है, मैं rspec में एकीकरण परीक्षण लिखना चाहता हूं। विकी में जानकारी का उपयोग करके, मैंने निम्नलिखित लिखा है, लेकिन मुझे पता है कि मुझे चीजें याद आ रही हैं।रेल आरएसपीसी और ओमनीथ (एकीकरण परीक्षण)

config/वातावरण में test.rb के तहत, मैं निम्नलिखित लाइनों है

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 बुला का नाम है रेल कंसोल में स्ट्रिंग ठीक देता है।

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

किसी को भी मदद कर सकते हैं मुझे यह पता लगाने कैसे OmniAuth mocks कि प्रदान की जाती हैं उपयोग करने के लिए:

मैं जब मैं इस परीक्षण चलाने निम्न त्रुटि हो रही है? के लिए एक स्पष्टीकरण क्यों और कैसे यह काम भी सराहना की जाएगी।

उत्तर

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] 
कहीं अपने testcase में

क्या ज़रूरत है?

+1

हैलो! मैंने request.env ["omniauth.auth"] की कोशिश की, लेकिन मुझे त्रुटि मिली "शून्य वर्ग के लिए अपरिभाषित विधि env" – izumeroot

+0

बीटीडब्ल्यू, यहां सही समाधान है https://gist.github.com/kinopyo/1338738 – izumeroot

6

क्या आपने इन दो पंक्तियों को 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 की ओर (हालांकि यह एकीकरण परीक्षण वेब पेज में है) मैं केवल कोला के साथ काम करने में कामयाब रहा हूं अगर मेरे पास मेरे प्राधिकरण मॉडल का फैक्टरीगर्ल है जिसमें असली रहस्य और टोकन शामिल है (उदाहरण के लिए एक वैध फेसबुक लॉगिन से कॉपी और पेस्ट किया गया है संक्रमण) – Ben

1

मैं अत्यधिक सुझाव है this answer

संक्षेप में ...

  • सेट करें नकली
  • अनुरोध
  • टेस्ट जो कुछ कोड कॉलबैक

उदाहरण के लिए से जुड़ा हुआ है बनाओ: सत्र [ 'यूआईडी'] का गठन किया गया परीक्षण (हालांकि, मैं केवल क्या उपयोगकर्ता देखता परीक्षण करने के लिए चुनते हैं, या, बल्कि, नहीं देखता)

मेरे कोड ...

config/वातावरण/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

संबंधित मुद्दे