unit-testingruby-on-rails-3fixturesmachinist

Using machinist instead of fixtures


In my Rails 3 application, I have a User model with the following fields

   name: string
   email: string
   children: has_many association to another model

I'm using machinist 2 to generate mock data, its blueprint looks like

User.blueprint do
   name { 'user{sn}' }
   email { '{object.name}@domain.com' }
end

And User's Unit Test:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  should have_many( :children )
  should validate_uniqueness_of( :email )
  should_not allow_value("blah").for(:email)
  should_not allow_value("b lah").for(:email)
  should allow_value("a@b.com").for(:email)
  should allow_value("asdf@asdf.com").for(:email)
end

When I generated the user model, it created a fixture file. My understanding is that when I run rake, Rails uses that fixture file to generate objects used in the tests. Which is not what I want. I want Rails to use machinist's blueprints just a seamlessly as it uses the fixtures file.

Is there a way to do this? Is there some way to tell rails that it needs to use blueprints instead of fixtures?


Solution

  • Add this to config/application.rb:

    config.generators do |g|
      g.fixture_replacement :machinist
    end
    

    You can safely trash the old fixtures folder too, unless you want to keep them obviously!