0

我写了一个测试来声明某些请求只能由登录用户执行,他们将使用Devistic & Omniauth的Google OAuth2系统登录。我不能找到一种方法来嘲笑omniauth到返回登录的用户,从Omniauth Wiki page about Integration Tests support获取omniauth模拟登录请求规格在轨道

拿起例如这里的规范

describe "allows logged in users" do 
    before(:each) do 
      OmniAuth.config.test_mode = true 
      OmniAuth.config.add_mock(:google, {:uid => '12345'}) 
      Rails.application.env_config["devise.mapping"] = Devise.mappings[:user] 
      Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google] 
    end 

    it "new certification form" do 
     get new_certification_path 
     expect(response).to be_success 
    end 

    it "to create certification" do 
     certification_attributes = FactoryGirl.attributes_for :certification 
     expect { 
      post "/certifications", params: { certification: certification_attributes } 
     }.to change(Certification, :count) 

     expect(response).to redirect_to certification_path 
    end 

    end 

不用说我认不出太多的为什么还是它是从哪里失败给定的错误,我想这是因为用户无法登录

Failures: 

    1) Certifications allows logged in users new certification form 
    Failure/Error: expect(response).to be_success 
     expected `#<ActionDispatch::TestResponse:0x007fb23b4c2990 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mu..., @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>>.success?` to return true, got false 
    # ./spec/requests/certifications_spec.rb:49:in `block (3 levels) in <top (required)>' 

    2) Certifications allows logged in users to create certification 
    Failure/Error: 
     expect { 
     post "/certifications", params: { certification: certification_attributes } 
     }.to change(Certification, :count) 

     expected #count to have changed, but is still 0 
    # ./spec/requests/certifications_spec.rb:54:in `block (3 levels) in <top (required)>' 

回答

0

我是使用了错误的配置

这个进入spec/support/rails_helper

OmniAuth.config.test_mode = true 
    OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({ 
     :provider => "google_oauth2", 
     :uid => "123456789", 
     :info => { 
     :name => "Tony Stark", 
     :email => "[email protected]" 
     }, 
     :credentials => { 
     :token => "token", 
     :refresh_token => "refresh token" 
     } 
    } 
) 

,然后方法之前

before(:each) do 
     Rails.application.env_config["devise.mapping"] = Devise.mappings[:user] 
     Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2] 
end