Rspec Mail object does not respond to include? - ruby-on-rails

I'm trying to learn testing in rails with the help of this screencast. But I get stuck with this step
expect(last_email).to include(user.email)
The error message is
expected #<Mail::Message:47172420, Multipart: true, Headers: <Date: Sun, 08 Feb 2015 09:51:56 +0530>, <From: from#example.com>, <To: person1#example.com>, <Message-ID: <54d6e464682e_1450a1b32815670#User-PC.mail>>, <Subject: Password Reset>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_54d6e4642e9e_1450a1b328155a"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>> to include "person1#example.com", but it does not respond to `include?`
The mail object does not respond to 'include?'. Could this be due to a change in the rspec syntax or is it something wrong with the code. Thanks for the answers in advance.

Looks like you're not calling to on last_email:
expect(last_email.to).to include(user.email)

Related

Attempt to send an email via Rails ActionMailer results in a failed call to `normalize_name` inside `lookup_context.rb`

I have a Mailer class which inherits from ApplicationMailer, which in turn inherits from ActionMailer::Base. Ruby version is ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15].
The Mailer class looks like the following:
class PurchaseOrderStatusMailer < ApplicationMailer
CONTACTS = {
JC: ['me#example.com'],
RM: ['me#example.com']
}
def daily_report_email(facility)
#facility = facility
ingredient_items = LineItem.ingredient.by_facility(facility)
#delivered_count = ingredient_items.by_date([7.days.ago, 7.days.from_now]).delivered.count
#partial_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).partial.count
#late_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).late.count
#expected_count = ingredient_items.by_date([Date.today, 7.days.from_now]).expected.count
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}")
end
end
ApplicationMailer looks like the following:
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'notify#example.com'
def facility_email(facility)
emails = Rails.application.config_for(:emails)
(emails[facility] + emails["DEFAULT"]).flatten
end
end
The view is located at app/views/purchase_order_status_mailer/daily_report_email.html.erb.
When I open my Rails console and type PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver, I see the following error:
NoMethodError: undefined method `empty?' for nil:NilClass
from /Users/me/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionview-5.0.4/lib/action_view/lookup_context.rb:215:in `normalize_name'
I tried passing a format block to the call to the mail helper with the same call, like so:
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}") do |format|
format.text { render plain: "Hey!" }
end
The above produced the following response, which appears to represent a successful email send:
Rendering text template
Rendered text template (0.0ms)
Sent mail to me#example.com (8.8ms)
Date: Mon, 25 Sep 2017 12:55:11 -0400
From: notify#example.com
To: me#example.com
Message-ID: <59c934efec401_1115b3fd69cc3f840917be#me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hey!
=> #<Mail::Message:70191029273300, Multipart: false, Headers: <Date: Mon, 25 Sep 2017 12:55:11 -0400>, <From: notify#example.com>, <To: ["me#example.com"]>, <Message-ID: <59c934efec401_1115b3fd69cc3f840917be#me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
I didn't actually receive an email, and I assume that means I don't have SMTP set up on my local machine, but the above response is encouraging. There was no stack trace apart from the error I posted above, so I tried digging into the Rails source code, and I saw that normalize_name inside lookup_context.rb gets called from within the args_for_lookup protected method, which in turn gets called by the ViewPaths module's find_all method. But beyond that it was hard to trace the call stack, as I couldn't find who the caller of find_all is.
My question is: what is wrong with the first call to mail?
EDIT 1: I also tried format.html { render html: "<h1>Hello Mikel!</h1>".html_safe } instead of the format.text option, as per the example here, and I got a similar success message.
I then tried adding a byebug statement inside normalize_name, to try and identify what the values of the parameters were on the successful email sends, but it looks like this method isn't getting called when a block is passed. That makes me suspect even more strongly that the problem is somehow related to my views. But I can't confirm that yet.
The issue is here
PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver
remove the new i.e
PurchaseOrderStatusMailer.daily_report_email('JC').deliver
Because of the wrong object of its not able to deduce the template path/file
I was able to generate a success response (including my mailer's rendered template file) by passing template_path and template_name options to the original call to the mail helper, like so:
mail(
to: CONTACTS[facility.to_sym],
subject: "#{facility} Daily Receipt Status - #{Date.today}",
template_path: 'purchase_order_status_mailer',
template_name: 'daily_report_email')
This generated the following success response:
Rendering purchase_order_status_mailer/daily_report_email.text.erb
Rendered purchase_order_status_mailer/daily_report_email.text.erb (0.3ms)
Sent mail to me#example.com (12.1ms)
Date: Mon, 25 Sep 2017 15:18:25 -0400
From: notify#example.com
To: me#example.com
Message-ID: <59c9568121cec_11e943fee8503f82823542#me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_59c9568120033_11e943fee8503f8282341e
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Please find the receipt status for JC as of 09-25-2017:
=================================================================================================
...<lots more content, including ERB rendered into HTML>...
=> #<Mail::Message:70293683934580, Multipart: true, Headers: <Date: Mon, 25 Sep 2017 15:18:25 -0400>, <From: notify#example.com>, <To: ["me#example.com"]>, <Message-ID: <59c9568121cec_11e943fee8503f82823542#me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
I'm a little confused about why I had to add these options, as the documentation doesn't make it clear that these are required and my templates and folders appear to be named correctly, but it worked and I have deadlines so I'm moving on. :-)

Adding bcc malfunctioning - Rails

I just came across a problem, spent hours but couldn't found root cause.
Mail sending works fine if I don't add bcc option but when I add bcc,
def test_email
mail :to => 'recipient#xyz.com',
:subject => 'Sample test',
:bcc => ['bcc#xyz.com']
end
It sends email to none and adds recipient#xyz.com as bcc.
Here is console output,
Mailer.test_email.deliver
#<Mail::Message:63045380, Multipart: true, Headers: <Date: Mon, 02 Jun 2014 13:03:02 +0000>, <From: hostmail#sample.dk>, <To: >, <Cc: >, <Bcc: ["recipient#xyz.com"]>, <Message-ID: <538c760686568_45b9a1001872969#DANVA-test-NAGIOS-monitor.mail>>, <Subject: Sample test>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_538c76063914_45b9a1001872662"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>, <X-Auto-Response-Suppress: OOF>, <Auto-Submitted: auto-generated>>
I am using Rails 3.2.8 and ruby 1.9.3p545.
I ideas to fix this prblem?

Attachment not sent properly in rails using ActionMailer

I am trying to send xls file via ActionMailer.
mailer = ActionMailer::Base.mail(:to => "reciever#gmail.com", :from => "sender#gmail.com", :content_type=>"application/vnd.ms-excel", :body=> '')
mailer.attachments['filename.xls']= {mime_type: 'application/vnd.ms-excel', content: File.read("filePath.xls")}
mailer.deliver!
I am able to receive the mail as well.
But somehow the attachment is not correct, it shows up as noname and below is the content I get in the file (I am copy pasting the exact contents)
--
Date: Wed, 04 Jun 2014 23:33:48 +0530
Mime-Version: 1.0
Content-Type: application/vnd.ms-excel;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename=filename.xls
Content-ID: <538f5f82836992#C02L2178FFT3.gmail>
PGgzIHN0eWxlPSJmb250LXdlaWdodDpib2xkIj4gCiAgICBTaG93aW5nCiAg
ICBvcGVuCiAgICByZXF1ZXN0cwogICAgZnJvbQogICAgTm92IDIxLCAyMDEz
....
I am sure I am missing something simple, I am unable to figure out what. Can someone help?
Try this:--
mailer = ActionMailer::Base.mail(:to => "reciever#gmail.com", :from => "sender#gmail.com", :content_type=>"application/vnd.ms-excel", :body=> '')
mailer.attachments["filename.xls"]= File.read("filePath.xls")
mailer.deliver!

Rails 3.0.17 sending inline attachments instead of regular ones

So, we have just upgraded our project to Rails 3.0.17 (please don't ask why this particular version, just happened so) on top of Ruby 1.8.7 enterprise edition.
I have methods to generate CSV data and send it as a file via email.
# contact_mailer.rb
def send_payments_report
attachments["report.csv"] = {
:content => Payment.generate_csv_report,
:mime_type => "text/csv"
}
mail(
:from => "from#example.com",
:subject => "Payments report",
:to => "to#example.com"
)
end
Here's what it returns:
=> #<Mail::Message:2231891440, Multipart: true, Headers: <Date: Wed, 19 Dec 2012 11:48:05 +0200>, <From: from#example.com>, <To: to#example.com>, <Message-ID: <50d18d555dcdc_2269838ab93812784#artem.local.mail>>, <Subject: Payments report>, <Mime-Version: 1.0>, <Content-Type: multipart/mixed; >, <Content-Transfer-Encoding: 7bit>>
The email itself is displayed as plain text without attachments:
--
Date: Wed, 19 Dec 2012 11:48:05 +0200
Mime-Version: 1.0
Content-Type: text/csv;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=report.csv
Content-ID: <50d18d555dcdc_2269838ab93812784#artem.local.mail>
"customer id","reference nr","invoice id","invoice type","invoice sum","sum paid","payment date"
"1035","010294","7113","Fine","250.00","100.00","2012-11-15"
"1035","010294","6132","Fine","250.00","250.00","2012-11-15"
"1035","010294","5241","Fine","850.00","650.00","2012-11-15"
--
I tried attaching existing file, but it displays it the same way. It worked perfectly on Rails 2.3.9. Can anyone help on that?
I seem to have solved this problem.
Here how I call the mail method:
mail(
:from => "from#example.com",
:subject => "Payments report",
:to => "to#example.com"
) do |format|
format.text {render :text => ""}
end

How to test email headers using RSpec

I'm using SendGrid's SMTP API in my Rails application to send out emails. However, I'm running into troubles testing the email header ("X-SMTPAPI") using RSpec.
Here's what the email looks like (retrieving from ActionMailer::Base.deliveries):
#<Mail::Message:2189335760, Multipart: false, Headers:
<Date: Tue, 20 Dec 2011 16:14:25 +0800>,
<From: "Acme Inc" <contact#acmeinc.com>>,
<To: doesntmatter#nowhere.com>,
<Message-ID: <4ef043e1b9490_4e4800eb1b095f1#Macbook.local.mail>>,
<Subject: Your Acme order>, <Mime-Version: 1.0>,
<Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>,
<X-SMTPAPI: {"sub":{"|last_name|":[Foo],"|first_name|":[Bar]},"to":["foo#bar.com"]}>>
Here's my spec code (which failed):
ActionMailer::Base.deliveries.last.to.should include("foo#bar.com")
I've also tried various method to retrieve the header("X-SMTPAPI") and didn't work either:
mail = ActionMailer::Base.deliveries.last
mail.headers("X-SMTPAPI") #NoMethodError: undefined method `each_pair' for "X-SMTPAPI":String
Help?
Update (answer)
Turns out, I can do this to retrieve the value of the email header:
mail.header['X-SMTPAPI'].value
However, the returned value is in JSON format. Then, all I need to do is to decode it:
sendgrid_header = ActiveSupport::JSON.decode(mail.header['X-SMTPAPI'].value)
which returns a hash, where I can do this:
sendgrid_header["to"]
to retrieve the array of email addresses.
The email_spec gem has a bunch of matchers that make this easier, you can do stuff like
mail.should have_header('X-SMTPAPI', some_value)
mail.should deliver_to('foo#bar.com')
And perusing the source to that gem should point you in the right direction if you don't want to use it e.g.
mail.to.addrs
returns you the email addresses (as opposed to stuff like 'Bob ')
and
mail.header['foo']
gets you the field for the foo header (depending on what you're checking you may want to call to_s on it to get the actual field value)
Repeating some of the other advice here, in more modern rspec syntax:
RSpec.describe ImportFile::Mailer do
describe '.file_error' do
let(:mail) { described_class.file_error('daily.csv', 'missing header') }
it { expect(mail.subject).to eq("Import error: missing header in daily.csv") }
it { expect(mail.header['X-source-file'].to_s).to eq ('daily.csv') }
end
end

Resources