Blog

Thoughts from my daily grind

RSpec HTML match - HTML escape related error

Posted by Ziyan Junaideen |Published: 18 May 2022 |Category: Ruby on Rails
Default Upload |

Pushing updates to Github, I encountered an error that I didn't experience locally. The issue was related to the apostrophe (') getting encoded as '. RSpec checks for a ' but finds ' causing an error. If we HTML escape the name before checking it with the mail body, the error should happen no more.

We can CGI.escapeHTML the name in similar cases and avoid such errors.

requrie 'cgi'

RSpec.describe UserMailer, type: :mailer do
  it "sends welcome mail" do
    user = create(:user, last_name: "O'Reilly")
    mail = UserMailer.with(user: user).welcome

    expect(mail.body.encoded).to match(CGI.escapeHTML(user.first_name)
  end
end

This error and similar errors I have experienced over the last few years add to my concerns about using FFaker library to generate random names. I wouldn't say I like tests to fail randomly.

About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments