2011-11-29 1 views
7

मैं ट्विटर मणि का उपयोग कर एक टेस्ट ऐप लिख रहा हूं और मैं एकीकरण परीक्षण लिखना चाहता हूं लेकिन मैं यह नहीं समझ सकता कि ट्विटर नामस्थान में ऑब्जेक्ट्स का नकल कैसे करें।रूबी में किसी तृतीय पक्ष ऑब्जेक्ट को नकल करने का सबसे अच्छा तरीका क्या है?

1) testing oauth twitter 
    Failure/Error: login_with_oauth 
    Twitter::Error::Unauthorized: 
    GET https://api.twitter.com/1/account/verify_credentials.json: 401: Invalid/expired Token 
    # ./app/models/user.rb:40:in `build_twitter' 
    # ./app/models/user.rb:16:in `build_authentication' 
    # ./app/controllers/authentications_controller.rb:47:in `create' 
    # ./spec/support/integration_spec_helper.rb:3:in `login_with_oauth' 
    # ./spec/integration/twit_test.rb:16:in `block (2 levels) in <top (required)>' 
: मैं इस त्रुटि मिल रही है,

feature 'testing oauth' do 
    before(:each) do 
    @twitter = double("Twitter") 
    @twitter.stub!(:configure).and_return true 
    @client = double("Twitter::Client") 
    @client.stub!(:current_user).and_return(@user) 
    @user = double("Twitter::User") 
    @user.stub!(:name).and_return("Tester") 
    end 

    scenario 'twitter' do 

    visit root_path 
    login_with_oauth 

    page.should have_content("Pages#home") 
    end 
end 

लेकिन: यहाँ समारोह है कि मैं परीक्षण करना चाहते हैं:

def build_twitter(omniauth) 
    Twitter.configure do |config| 
    config.consumer_key = TWITTER_KEY 
    config.consumer_secret = TWITTER_SECRET 
    config.oauth_token = omniauth['credentials']['token'] 
    config.oauth_token_secret = omniauth['credentials']['secret'] 
    end 
    client = Twitter::Client.new 
    user = client.current_user 
    self.name = user.name 
end 

और यहाँ rspec परीक्षण है कि मैं लिखने के लिए कोशिश कर रहा हूँ है

ऊपर दिए गए मिक्स rspec का उपयोग कर रहे हैं लेकिन मैं भी मोचा की कोशिश करने के लिए खुला हूं। किसी भी तरह की सहायता का स्वागत किया जाएगा।

ठीक है, मैं इसे हर किसी की मदद के लिए धन्यवाद देने में कामयाब रहा। यहाँ अंतिम परीक्षण है:

feature 'testing oauth' do 
    before(:each) do 
    @client = double("Twitter::Client") 
    @user = double("Twitter::User") 
    Twitter.stub!(:configure).and_return true 
    Twitter::Client.stub!(:new).and_return(@client) 
    @client.stub!(:current_user).and_return(@user) 
    @user.stub!(:name).and_return("Tester") 
    end 

    scenario 'twitter' do 

    visit root_path 
    login_with_oauth 

    page.should have_content("Pages#home") 
    end 
end 

चाल पता लगाना था कि मैं असली वस्तुओं पर :configure और :new ठूंठ करने की जरूरत है और एक dobuled वस्तु दृष्टान्त पर :current_user और :name ठूंठ।

उत्तर

4

मुझे लगता है कि समस्या यह है कि आप नकली का उपयोग कर रहे हैं, आपने नकली @ ट्वीटर बनाया है, लेकिन आप वास्तव में इसका उपयोग कभी नहीं करते हैं। मुझे लगता है कि आप इस धारणा के तहत हो सकते हैं कि ट्विटर पर कोई भी कॉल आपके द्वारा निर्दिष्ट स्टब किए गए तरीकों का उपयोग करेगा, लेकिन ऐसा नहीं है कि यह कैसे काम करता है, केवल @ ट्वीटर को कॉल किया जाता है।

मैं डबल रूबी, rspec नहीं mocks उपयोग करें, लेकिन मेरा मानना ​​है कि आप इस के बजाय की तरह कुछ करना चाहता हूँ:

Twitter.stub!(:configure).and_return true 
... 
Twitter::Client.stub!(:current_user).and_return @user 

यह सुनिश्चित करता है कि कभी भी तरीकों आप ट्विटर पर टोंटदार, ट्विटर :: क्लाइंट कहा जाता है, वे जवाब देते हैं कि आप कैसे चाहते हैं।

इसके अलावा, यह अजीब लगता है कि यह एक दृश्य के हिस्से के रूप में परीक्षण किया जाता है, वास्तव में एक नियंत्रक परीक्षण का हिस्सा होना चाहिए, जब तक कि मुझे कुछ याद न हो।

+0

हाँ! धन्यवाद। आप बिल्कुल सही हैं कि मॉडल पर एक विधि को दबाकर और दोगुना हुआ एक उदाहरण छीनने के बीच मुझे अंतर याद आ रहा था। इसके लिए धन्यवाद मैं इसे समझने में सक्षम था। मैं इस सवाल को संपादित करने के लिए संपादित करूंगा कि मैंने अंत में इसे कैसे हल किया। – spinlock

0

आप http://jondot.github.com/moxy/ जैसे कुछ का उपयोग करने का प्रयास कर सकते हैं। नकली वेब अनुरोध