2012-05-06 31 views
5

Tôi đã nhìn vào wiki đá quý và theo các hướng dẫn nhưng đối với một số lý do tôi nhận được một nil khi thực hiện một thử nghiệm omniauth:Làm thế nào để bạn kiểm tra facebook omni-auth bằng rspec?

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) 

kết quả spec:

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)>' 

Trả lời

4

Hãy thử một biểu tượng trong thử nghiệm của bạn chứ không phải là một chuỗi:

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

Tôi không chắc chắn nếu Omniauth thay đổi từ chuỗi các biểu tượng tại một số điểm nhưng có vẻ là một số ví dụ ra có sử dụng chuỗi như là chìa khóa .

Chỉ để hoàn thành, nếu có ai đó đang cố gắng làm điều này với Devise và Omniauth, đừng quên thêm devise.mapping vào khối trước.

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

Điều này sẽ được thực hiện như thế nào nếu bạn không sử dụng Devise? – freddyrangel

Các vấn đề liên quan