如何解决在rspec测试登录期间意外和随机失败302
尝试测试用户登录的API时,在用户登录时出现了302而不是200的随机错误。
有时测试通过绿色测试,有时却失败,并显示以下响应:
<html><body>You are being <a href="http://www.example.com/en">redirected</a>.</body></html>
require 'rails_helper'
RSpec.describe Api::V1::SessionsController,type: :request do
let(:user) { create(:user) }
let(:signin_url) { '/api/v1/signin' }
let(:user_attributes) { { user: { email: user.email,password: user.password } }.to_json }
let(:headers) { { 'Accept' => 'application/json','Content-Type' => 'application/json' } }
context 'When sign in as an existing user' do
before do
user.confirm
post signin_url,headers: headers,params: user_attributes
end
it 'returns 200' do
expect(response.status).to eq(200)
end
end
end
这是我的控制器动作
before_action :sign_in_params,only: :create
before_action :load_user,only: :create
skip_before_action :verify_authenticity_token,only: [:create,:destroy]
def create
if @user.valid_password?(sign_in_params[:password])
sign_in 'user',@user
render json: {
message: 'Sign in successful',user: @user
},status: :ok
else
render json: {
message: 'Something went wrong',user: {}
},status: :unauthorized
end
end
def sign_in_params
params.require(:user).permit :email,:password
end
def load_user
@user = User.find_for_database_authentication(email: sign_in_params[:email])
@user || render(json: {
messages: 'Something went wrong',user: {}
},status: :unauthorized)
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。