mardi 17 octobre 2017

How to compile ERB in a CSV fixture with rspec / Rails 5?

I want to compile some ERB into a CSV inside a fixture for my spec. Here is the CSV:

(spec/fixtures/files/song_info.csv.erb)

song id,        song_title
<%= song.id %>, Fun Title

In my test, I first create a song so I can interpolate it's id into the fixture, then load the CSV.

describe "#update" do
  let(:song) { FactoryGirl.create :song }         # create the instance
  let(:csv) { file_fixture("song_info.csv.erb").read } # load the file

  it "finds a song and adds it's title" do        
    # when I look at csv here, it is just a string with the raw ERB 
  end
end

What happens inside the test is unimportant. The issue is that when I check out the contents of csv I see that it is just a string with the raw ERB (uncompiled).

i.e. "song_id, new_song_title, <%= song.id %>, Song Title"

How do I force the ERB to compile? Is #read not the right file_fixture method? Is it something else completely?

Note: I know there are other ways to accomplish this without fixtures, but this is a trivial example. I just want to know how to compile ERB into a fixture.

Aucun commentaire:

Enregistrer un commentaire