2012-05-06 11 views
5

मैं मणि विकि पर देखा है और निर्देशों का पालन किया, लेकिन जब एक omniauth परीक्षण कर रही है किसी कारण से मैं एक nil हो रही है:आप rspec का उपयोग कर omni-auth facebook का परीक्षण कैसे करते हैं?

user_sessions_controller_spec.rb:

require 'spec_helper' 

describe UserSessionsController, "OmniAuth" do 
    before do 
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] 
    end 

    it "sets a session variable to the OmniAuth auth hash" do 
    request.env["omniauth.auth"]['uid'].should == '123545' 
    end 
end 

spec_helper.rb:

RACK_ENV = ENV['ENVIRONMENT'] ||= 'test' 
OmniAuth.config.test_mode = true 
omniauth_hash = 
    {:provider => "facebook", 
    :uid  => "1234", 
    :info => {:name  => "John Doe", 
       :email  => "[email protected]"}, 
    :credentials => {:token => "testtoken234tsdf"}} 

OmniAuth.config.add_mock(:facebook, omniauth_hash) 

कल्पना परिणाम:

Failures: 

    1) UserSessionsController OmniAuth sets a session variable to the OmniAuth auth hash 
    Failure/Error: request.env["omniauth.auth"]['uid'].should == '123545' 
    NoMethodError: 
     You have a nil object when you didn't expect it! 
     You might have expected an instance of Array. 
     The error occurred while evaluating nil.[] 
    # ./spec/controllers/user_sessions_controller_spec.rb:10:in `block (2 levels) in <top (required)>' 

उत्तर

4

बल्कि एक स्ट्रिंग की तुलना में अपने परीक्षण में एक प्रतीक का प्रयास करें:

it "sets a session variable to the OmniAuth auth hash" do 
    request.env["omniauth.auth"][:uid].should == '1234' 
    end 

मुझे यकीन है कि अगर Omniauth कुछ बिंदु पर प्रतीकों को तार से बदल नहीं कर रहा हूँ लेकिन वहाँ कुंजी के रूप में तार का उपयोग कर उदाहरण के एक नंबर हो रहा है ।

बस पूर्णता के लिए, यदि कोई भी डेविस और ओमनिथ के साथ ऐसा करने का प्रयास कर रहा है, तो पहले ब्लॉक में devise.mapping को जोड़ना न भूलें।

before do 
    request.env["devise.mapping"] = Devise.mappings[:user] 
    request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] 
end 
+2

यदि आप डेविस का उपयोग नहीं कर रहे हैं तो यह कैसे किया जाएगा? – freddyrangel

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