Clag - not in active development - use machinist or factory_girl
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
.gitignore
CHANGELOG
MIT-LICENSE
README.rdoc
Rakefile
SPECDOC
TODO
garlic_example.rb
init.rb

README.rdoc

EDIT: Use machinist!

Use factory_girl - it does everything that Clag does!

And it has a load of people using it, and it rocks. I just spent an evening converting clag stuff over to factory_girl, and it's perfectly easy.

I'll be using 'clag' for some cucumber + factory_girl stuff that I've been working on, for now it remains the same old CLaG

Clag

Clag is the awesome edible glue that Australian kids know and love. In this context it's a fixture replacement thingy. For me, it speeds up my specing. It does put more load on the db, but if you've got a snazzy dev machine it won't matter.

Sometimes you want real ActiveRecords in your specs. You're supposed to set em up with fixtures. I hate fixtures, I keep having to look in different files, and they don't get created in the same way as a real ActiveRecord.

(Code inspired from replacefixtures.rubyforge.org/)

Coverage and CI

Clag is C0 100%, and is tested against 2.0.3, 2.0-stable, 2.1.0, 2.1-stable, and edge

Basic Example

With Clag, you can do stuff like this:

describe "A User with two posts" do
  before do
    @user = Clag.create_user!
    @post1 = Clag.create_post! :user => @user
    @post2 = Clag.create_post! :user => @user
  end

  ...
end

describe "A new post (with valid attributes)" do
  before do
    @post = Clag.new_post
  end

  it { @post.should be_valid }
end

The code required on your part is this (say in spec/clag.rb, which is included by spec/spec_helper)

require 'clag'

class Clag
  def user
    password = random
    {
      :email                  => unique_for(:user_email) {"#{random}@example.com"},
      :password               => password,
      :password_confirmation  => password,
      :display_name           => random,
      :forename               => random,
      :surname                => random
    }
  end

  def post
    {
      :title    => random,
      :body     => random,
      :user     => create_user!
    }
  end
end

You only need to edit this file when you create a new model, or change the attributes on a model.

Usage

Install this into plugins, or just stick clag.rb somewhere in a load path.

In spec_helper, or somewhere like that, reopen, or make a new class descended from Clag, and add tableized method names for your model attributes

Say:

class MyClag < Clag

  # if method sig has no options, then any extra ones are merged with the result
  def user
    { a hash is returned }
  end

  # if method sig has options, then it's assumed you will merge them here
  def area(options = {})
    { default hash }.merge(options)
  end
end

Then do MyClag.create_user, or MyClag.new_user, or MyClag.user_attributes (you can use shebangs as well)

Note: in the above example, the area method has an options hash. If you define it on the method, clag assumes you will take responsibility for merging any passed options into the resulting object.

You can make use of Clag methods like

random:     returns a random string

unique_for: guarantee a unique result when using a random value

  unique_for(:user_email) { "#{random}@email.com" }

Namespaces

You can use clag with namespaced models now, like this:

class Clag
  class Event < Clag
    def funky
      { :thing => 1 }
    end
  end
end

Clag.event.new_funky   # => Event::Funky.new :thing => 1

class MyClagWithExplicitNamespace < Clag
  self.namespace = [:awesome, :big]

  def thing
    { :name => 'greamme' }
  end
end

MyClagWithExplicitNamespace.create_thing!  # => Awesome::Big::Thing.create! :name => 'greamme'

Advantages of Clag

  • it's one tiny ruby file

  • you can group data together by scenarios or use cases by making a new clag class

  • all of the features of fixture replacement type thingies