October 19, 2007

Dan Manges mentions that the object factory that they’re using for model creation differs from Martin Fowler’s Object Mother in that Object Mother has specific objects. I can’t think of any reason that you couldn’t do something like this, though:

module Factory
  def self.create_user(attributes = {})
    default_attributes = {
      :username => "jdoe",
      :first_name => "John",
      :last_name => "Doe
    }
    User.create! default_attributes.merge(attributes)
  end

  def self.create_user_virgil(attributes = {})
    default_attributes = {
      :username => "virgil",
      :first_name => "Virgil",
      :last_name => "Doe
    }
    User.create! default_attributes.merge(attributes)
  end
end

and setting up the Virgil user to have a role of ‘administrator’ while user objects created with the create_user method have a default role of ‘user.’