2012-05-06 8 views
5

私は宝石のwikiを見て、指示に従ったが、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とOmniauthでこれをやろうとしている場合は、beforeブロックにdevise.mappingを追加することを忘れないでください。

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

Deviseを使用していない場合はどうすればよいですか? – freddyrangel

関連する問題