Search code examples
ruby-on-railsrubyfixturespaper-trail-gem

Rails YAML Fixture Error with PaperTrail gem


I'm trying to create a rails fixture for testing a controller that fetches versions persisted with the paper_trail gem (7.0.1). But, I've thus far been unable to populate any versions within fixtures to test against. And, it appears that the YAML file isn't loading/is breaking other tests. Here's the contents of my `versions.yml`` file:

---
version_001:
  id: 1
  item_type: 'DistributionChannel'
  item_id: 1
  event: 'create'
  whodunnit: <%= User.find_by(email: 'user@example.com').id.to_s %>
  object: nil
  created_at: 2017-05-15 12:00:00.000000000 Z
  object_changes: '---\\nid:\\n- \\n- 1\\nprimary_channel:\\n- \\n- Best Buy\\nsecondary_channel:\\n- \\n- ''\ncreated_at:\\n- \\n- 2017-05-15 12:00:00.000000000 Z\\nupdated_at:\\n- \\n- 2017-05-15 12:00:00.000000000 Z\\n'

The schema contains the versions table with the appropriate columns btw.


Solution

  • I eventually got the fixture to load by adding the following to test_helper.rb:

    self.set_fixture_class versions: PaperTrail::Version

    And, the with following yaml syntax:

    version_001:
      id: 1
      item_type: 'DistributionChannel'
      item_id: 1
      event: 'create'
      whodunnit: <%= ActiveRecord::FixtureSet.identify('user_example').to_s %>
      object: nil
      created_at: 2017-05-15 12:00:00
      object_changes: "---\nid:\n- \n- 1\nprimary_channel:\n- \n- Best Buy\nsecondary_channel:\n- \n- ''\ncreated_at:\n- \n- 2017-05-15 12:00:00.000000000 Z\nupdated_at:\n- \n- 2017-05-15 12:00:00.000000000 Z\n"
    

    The addition to test_helper.rb is necessary because the Version table is namespaced within PaperTrail. I've seen some conflicting info about appropriate yaml syntax, so I'm hesitant to explain my theories why that yaml syntax worked (any explanations in comments would be greatly appreciated). But, hopefully this will help point someone in the right direction when testing paper_trail with Minitest and using fixtures.