I try to test my mailer, but I have an error on my .count
,
Here is my 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
while I try to test with rspec this is what I'm doing
describe 'prices_mailer' do
let(:price) { create(:price) }
let(:recipient) { '[email protected]' }
let(:sender) { '[email protected]' }
let(:mail) { described_class.prices_mailer(recipient, price, sender_email) }
end
it 'renders the headers' do
expect(mail.subject).to eq('test')
end
When I run the specs, I have the following error :
NoMethodError: undefined method count for #<Price
Is anyone knows how to fix that ?
CodePudding user response:
Your mailer expects an array or ActiveRecord relation as the second argument:
def prices_mailer(recipient, prices, sender)
The argument name is plural and you're calling #count
on it.
But your specs are passing a single price:
described_class.prices_mailer(recipient, price, sender_email)
Either update your specs to send in an array:
described_class.prices_mailer(recipient, [price], sender_email)
or update your mailer to handle a single price:
def prices_mailer(recipient, prices, sender)
prices = Array(prices)
#...
Array(prices)
will handle arrays, AR relations, single values, ... for prices
.