[RUBY] [RSpec] What I got stuck when I wrote the mailer (password reset process) test

Recently, I've become accustomed to RSpec, and I've been alert, but I've got an error I've never seen, and I'm a little stuck, so I'd like to share it here. This is the story when I wrote a mailer (password reset process) test in the request specs!

error contents


 Failure/Error: expect(mail.body.encoded).to match user.reset_token

       expected "\r\n----==_mimepart_5b18de57c36e0_75293fd69de666b8448c9\r\nContent-Type: text/plain;\r\n charset=UTF...\nL2E+CgogIDwvYm9keT4KPC9odG1sPgo=\r\n\r\n----==_mimepart_5b18de57c36e0_75293fd69de666b8448c9--\r\n" to match "HAY"
       Diff:
       @@ -1,2 +1,32 @@
       -HAY
       +
       +----==_mimepart_5b18de57c36e0_75293fd69de666b8448c9
       +Content-Type: text/plain;
       + charset=UTF-8
       +Content-Transfer-Encoding: base64
       +
       +SEFZCuOBggoK
       +
       +----==_mimepart_5b18de57c36e0_75293fd69de666b8448c9
       +Content-Type: text/html;
       + charset=UTF-8
       +Content-Transfer-Encoding: base64
       +
       +PCFET0NUWVBFIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPG1ldGEgaHR0
       (abridgement)
       +L2E+CgogIDwvYm9keT4KPC9odG1sPgo=
       +
       +----==_mimepart_5b18de57c36e0_75293fd69de666b8448c9--

I got an error like this. Apparently, RSpec doesn't encode the email body correctly, so the test fails because it doesn't match the expected value.

solution

When I made the password reset token, it was encoded using Base64, so I put it back and compared it. (Decode)

password_resets_request_spec.rb


RSpec.describe UserMailer, type: :mailer do
  let(:user) { FactoryBot.create(:user, email: '[email protected]') }

  describe "Password reset process" do
    let(:mail) { UserMailer.password_reset(user) }
    #Base64 encode can be decoded and compared
    let(:mail_body) { mail.body.encoded.split(/\r\n/).map{|i| Base64.decode64(i)}.join }

    it "The header is displayed correctly" do
      user.reset_token = User.new_token
      expect(mail.to).to eq ["[email protected]"]
      expect(mail.from).to eq ["[email protected]"]
      expect(mail.subject).to eq "Password Reset"
    end

    #Email preview test
    it "The email text is displayed correctly" do
      user.reset_token = User.new_token
      expect(mail_body).to match user.reset_token
      expect(mail_body).to match CGI.escape(user.email)
    end
  end
end

Thank you for reading to the end!

Actually, it took me a while to get to this information, so I wrote it as an article.

I hope you can help those who are in trouble in the same place! If you have any suggestions, I would appreciate it if you could comment.

Recommended Posts

[RSpec] What I got stuck when I wrote the mailer (password reset process) test
I rewrote the Rails tutorial test with RSpec
I got stuck trying Volley's synchronization process (Java)
I got stuck when port forwarding with VBox
Rspec: I want to test the post-execution state when I set a method on subject
[RSpec] I wrote a test for uploading a profile image.
What I stumbled upon in the ActiveModel :: Serializer test
[RSpec] When I tested the view file that used the helper method, I got ActionView :: Template :: Error :. ..
What I was addicted to when introducing the JNI library