我在我的项目中将Ruby版本从2.7.3升级到3.2.0,一些测试开始失败,其中一个测试使用关键字参数调用函数,它抛出的错误如下:
1) NotificationsMailer notify Sets body
Failure/Error:
def notify(recipient_emails:, subject:, body:)
@body = body
@subject = subject
mail(bcc: recipient_emails, subject: @subject, reply_to: VENDOR_SUPPORT_EMAIL)
end
ArgumentError:
wrong number of arguments (given 1, expected 0; required keywords: recipient_emails, subject, body)
# ./app/mailers/notifications_mailer.rb:5:in `notify'
# /usr/local/bundle/gems/actiontext-6.1.6/lib/action_text/rendering.rb:20:in `with_renderer'
# /usr/local/bundle/gems/actiontext-6.1.6/lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
# ./spec/mailers/notifications_mailer_spec.rb:16:in `block (3 levels) in <top (required)>'
下面是被调用函数的定义:
class NotificationsMailer < ApplicationMailer
VENDOR_SUPPORT_EMAIL = "vendor-support@saatvamattress.com"
def notify(recipient_emails:, subject:, body:)
@body = body
@subject = subject
mail(bcc: recipient_emails, subject: @subject, reply_to: VENDOR_SUPPORT_EMAIL)
end
end
这是测试文件:
require "rails_helper"
RSpec.describe NotificationsMailer, type: :mailer do
describe 'notify' do
let(:recipient_emails) { %w[test@mail.com test2@mail.com] }
let(:mail) do
NotificationsMailer.notify(
recipient_emails: recipient_emails,
subject: 'subject',
body: 'body'
)
end
it 'Sets body' do
expect(mail.body.encoded).to match 'body'
end
it 'Sets subject' do
expect(mail.subject).to eq 'subject'
end
it "Does not set To" do
expect(mail.to).to be_nil
end
it "Does not set CC" do
expect(mail.cc).to be_nil
end
it "Sets BCC with recipients' emails" do
expect(mail.bcc).to eq ['test@mail.com', 'test2@mail.com']
end
it 'Sets reply_to with vendor support email' do
expect(mail.reply_to).to eq ['vendor-support@saatvamattress.com']
end
end
end
我知道ruby 3对关键字参数做了一些修改,但是当我用相同的顺序调用函数并指定所有的关键字时,我看不出问题出在哪里。
在一些线程之后,我尝试以散列形式发送参数和其他一些不太有希望的东西,但仍然得到错误,不知道那里发生了什么。
1条答案
按热度按时间laawzig21#
我能够重现你在Rails6.1.7.2和Ruby 3.2.1上的问题。问题是Rails 6.1和Ruby 3.2不完全兼容。有人试图将3.2兼容性修复程序移植到Rails 6.1上(参见https://github.com/rails/rails/pull/46895),但似乎还没有发布。
你可以升级到较低版本的Ruby(3.1.2对我来说很好用,我相信3.1.3也可以),或者你可以像这样修改你的邮件程序代码:
(and显然也改变了调用该函数的方式)。
但是,使用Rails 6.1和Ruby 3.2也有一定的风险,您可能会遇到更多与kwargs相关的问题,因此使用Ruby 3.1可能是更好的选择。