Embedded Ruby in text fixture

I'm testing the text part of an email in my Rails application and I have a text fixture to test against. The fixture is not a .yml file since it's just text to test against. However, the email contains the current year (like 2010-2018) and I'm trying to see if I can use erb in that text fixture so I can test my email without having to update it every year.

This is how I'm currently using the textfile:

textfile

...
Copyright 2006-2018

test file:

assert_equal read_fixture('textfile').join, email.text_part.body.to_s

1 answer

  • answered 2018-07-11 11:03 Leo

    When you use rails fixtures you can mix ERB in with your YAML fixtures(don't need to add "erb" extension to the filename):

    # fixtere_name.yml
    one:
      column: <%= DateTime.now %>
    

    When you use own fixtures or similar files you can manually execute ruby parts. For example:

    # fixtere.txt
    some text <%= DateTime.now.year %>
    
    # load this fixture
    fixture = ERB.new File.read("path/to/fixture.txt"), nil, "%"
    fixture.result(binding) # => "some text 2018\n"