无法将会话变量传递给RSpec控制器

时间:2019-04-29 12:04:18

标签: ruby-on-rails rspec

我正在使用RSpec从控制器测试确认方法,似乎测试失败了,因为在解决该方法时我无法传递会话变量。

这是我的规格:

        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

        <property name="supportedMediaTypes" value="application/json" />

方法确认电子邮件:

RSpec.describe OauthCallbacksController, type: :controller do
  describe 'POST #confirm_email' do
    before do
      request.env["devise.mapping"] = Devise.mappings[:user]
    end

    context 'confirms_email' do
      let(:email) { 'test-email@test.ru' }
      let(:confirm_email) { post :confirm_email, params: { email: email }, session: { auth: { uid: '12345', provider: 'facebook' } } }

      it 'redirects to new_user_session_path' do
        confirm_email
        expect(response).to redirect_to new_user_session_path
      end

      it 'sends an email' do
        expect { confirm_email }.to change{ ActionMailer::Base.deliveries.count }.by(1)
      end
    end
  end
end

当我尝试显示会话变量时,它们是def confirm_email puts params[:email] pending_user = User.find_or_init_skip_confirmation(params[:email]) if pending_user aut = Authorization.where(provider: session[:auth]['provider'], uid: session[:auth]['uid'], linked_email: params[:email]) .first_or_initialize do |auth| auth.user = pending_user auth.confirmation_token = Devise.friendly_token[0, 20], auth.confirmation_sent_at = Time.now.utc end if aut.save OauthMailer.send_confirmation_letter(aut).deliver_now redirect_to root_path, notice: "Great! Now confirm your email, we've sent you a letter!" else redirect_to root_path, alert: "Something went wrong. Please try again later or use another sign in method" end end end ,在test.log中,很明显应该有会话变量为NULL-s。

  

[34mSELECT“授权”。 “ id” ASC LIMIT $ 2 [0m [[“” link_email“,” test-email@test.ru“],[” LIMIT“,1]]

我使用Rails 5.2.2.1和RSpec 3.8。

我已经阅读到Rails 5中存在访问会话的问题,我已经尝试使用nil进行设置,或者 controller.session[:auth]['uid'],但不重要

2 个答案:

答案 0 :(得分:0)

在滑轨或机架中发生了一些奇怪的事情,但是在嵌套会话的情况下,按键可用作符号。这应该起作用

Authorization.where(provider: session[:auth][:provider], uid: session[:auth][:uid], linked_email: params[:email])

答案 1 :(得分:0)

我设法使用request.env["omniauth.auth"] = mock_auth_hash(:facebook)传递会话参数,并且当我没有传递哈希值时,我已经将请求传递给了会话。它完成了工作。 现在我的规格看起来像这样。

  describe 'POST #confirm_email' do
    before do
      request.env["devise.mapping"] = Devise.mappings[:user]
      request.env["omniauth.auth"] = mock_auth_hash(:facebook)
      get :facebook
    end

    let(:confirm_email) { post :confirm_email, params: { email: 'mockuser@test.com'}, session: { auth: request.env["omniauth.auth"] } }

    it 'redirects to root_path' do
      confirm_email
      expect(response).to redirect_to root_path
    end

    it 'sends an email' do
      request.env["devise.mapping"] = Devise.mappings[:user]
      request.env["omniauth.auth"] = mock_auth_hash(:facebook)
      get :facebook

      expect { confirm_email }.to change{ ActionMailer::Base.deliveries.count }.by(1)
    end
  end