Ever find yourself writing out the same old column names in fixtures just so your tests will work? Ever wonder if there’s a better way? Well there is!

Fixtures, now with NEW default columns!

All you need to do is bung a method in your test/test_helper.rb file that looks like this:

  
  def default_columns
    columns = <  

Now, in each fixture that requires the default columns put this at the top:

  
  <% require "#{RAILS_ROOT}/test/test_helper.rb" %>

And then for each fixture bung in this inline code:

  
  one:
  id: 1
  name: MyString
  <%= default_columns %> 

Voila! No more repeating yourself with stupid columns! Hooray!

Note: spacing is important in yaml files – so make sure you put the <%= default_columns %> in an appropriate place

Need to find a lawyer for your business? Get in touch with my company Lexoo and we'll find you a great one for free.