NoMethodError: undefined method count in rspec while testing my mailer

NoMethodError: undefined method count in rspec while testing my mailer

我尝试测试我的邮件程序,但我的 .count 出现错误, 这是我的 Mailer.rb

def prices_mailer(recipient, prices, sender)
    @sender = sender.fullname
    email = recipient.email
    @name = recipient.fullname
    @prices = prices
    @prices_count = @prices.count

@subject = 'test'
set_meta_data(__method__)
mail(to:email, subject: @subject, from: sender)
end

当我尝试使用 rspec 进行测试时,这就是我正在做的事情

describe 'prices_mailer' do
    let(:price) { create(:price) }
    let(:recipient) { 'John.doe2@gmail.com' }
    let(:sender) { 'John.doe@gmail.com' }
    let(:mail) { described_class.prices_mailer(recipient, price, sender_email) }
end

it 'renders the headers' do
      expect(mail.subject).to eq('test')
end

当我 运行 规范时,出现以下错误: NoMethodError: undefined method count for #<Price

有人知道如何解决这个问题吗?

您的邮件程序需要数组或 ActiveRecord 关系作为第二个参数:

def prices_mailer(recipient, prices, sender)

参数名称是复数,您正在对其调用 #count

但您的规格正在通过单一价格:

described_class.prices_mailer(recipient, price, sender_email)

更新您的规格以发送数组:

described_class.prices_mailer(recipient, [price], sender_email)

或更新您的邮件程序以处理单一价格:

def prices_mailer(recipient, prices, sender)
  prices = Array(prices)
  #...

Array(prices) 将为 prices.

处理数组、AR 关系、单个值...