gpt4 book ai didi

ruby-on-rails - Rails 测试设计 registrations_controller

转载 作者:行者123 更新时间:2023-11-28 21:37:19 25 4
gpt4 key购买 nike

我只希望拥有特定电子邮件的人能够注册。我正在使用 Devise,这是我的内部 Controller /admins/registrations_controller.rb

class Admins::RegistrationsController < Devise::RegistrationsController
def create
if params[:admin][:email] == Rails.application.credentials.development[:admin_email]
super
end
end

我正在尝试在测试中发送电子邮件、密码和确认密码。

require 'rails_helper'
RSpec.describe Admins::RegistrationsController do
it 'does not allow xxx@gmail.com with very-secret password to register' do
@request.env["devise.mapping"]=Devise.mappings[:admin]
patch :create, admin: {email: 'xxx@gmail.com", password: "very-secret", password-confirmations: "very-secret"}
end
end

我收到参数错误:未知关键字:admin。如何为 Devise 编写正确的补丁请求?

编辑:我的路线.rb

Rails.application.routes.draw do
devise_for :admins, controllers: {sessions: 'admins/sessions',
registrations: 'admins/registrations'}
....

我的佣金路线

admin_registration PATCH  /admins(.:format) 
admins/registrations#update
PUT /admins(.:format)
admins/registrations#update
DELETE /admins(.:format)
admins/registrations#destroy
POST /admins(.:format)
admins/registrations#create

更多信息:这是使用浏览器打印出来的,它在浏览器中确实有效:

  Started POST "/admins" for ::1 at 2019-7-03 21:08:58 -0500
Processing by Admins::RegistrationsController#create as HTML
Parameters: {"utf8"=>"_", "authenticity_token"=>"xxx", "admin"=>{"email"=>"abc@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
inside registrations_controller.rb CREATE

最佳答案

post :create, params: {"admin"=>{"email"=>"abc@gmail.com", "password"=>"very-secret", "password_confirmation"=>"very-secret"}}

关于ruby-on-rails - Rails 测试设计 registrations_controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56878236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com