You may have known that Ruby on Rails can protect email addresses that you have on your web pages with the mail_to view helper. But how do you test email obfuscation in your RSpec view tests?
To obfuscate an email address in a rails view, you can simply add encode: “javascript” to your mail_to helper.
1
| |
To test this, I created a method in my spec/spec_helper.rb file. This method goes through each character (byte) of the string containing the email address, and calls the sprintf method to format the character in the way that the javascript decoder in rails can read.
1 2 3 4 5 6 7 | |
Having that method available makes my example very easy to read:
1 2 3 | |
Do you know a better way? For example, hooking directly into the view helper code from ActionView::Helpers::UrlHelper#mail_to? Let me know in the comments.