You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rspec密码重置测试报错及后续参数错误问题求助

问题描述
  • 初始问题:验证密码重置成功后跳转首页的Rspec测试报错:Expected response to be a <3XX: redirect>, but was a <200: OK>
  • 后续问题:修复HTTP状态错误后,出现ArgumentError: wrong number of arguments (given 1, expected 0),报错代码行:PasswordReset.delete_all(user_email: user.email)
解决方案

1. 修复ArgumentError错误

ActiveRecord的delete_all方法在多数Rails版本中,不支持直接传入哈希条件作为参数,正确写法需先通过where筛选目标记录再执行删除:

# 错误写法
PasswordReset.delete_all(user_email: user.email)

# 正确写法
PasswordReset.where(user_email: user.email).delete_all

2. 修复跳转返回200而非3XX的问题

  • 检查控制器响应逻辑:
    确认密码重置action(如update)在密码修改成功后,存在正确的redirect_to语句,避免因验证失败进入render分支返回200:
    def update
      # 重置令牌校验、密码更新逻辑...
      if user.update(password: params[:password], password_confirmation: params[:password_confirmation])
        redirect_to root_path, notice: '密码重置成功'
      else
        # 若进入此分支会返回200,需确保测试触发的是成功路径
        render :edit
      end
    end
    
  • 校准测试前置条件:
    确保测试中传入的重置令牌有效、密码参数符合用户模型的验证规则(如长度、复杂度),避免因验证失败导致未触发跳转:
    it 'redirects to home page after successful password reset' do
      post password_reset_path, params: {
        token: valid_reset_token, # 确保此令牌有效且未过期
        password: 'valid_new_password123', # 符合密码验证规则
        password_confirmation: 'valid_new_password123'
      }
      expect(response).to redirect_to(root_path)
    end
    

内容的提问来源于stack exchange,提问作者palacetrading

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 11:05:22