Kathy Lee

An easy to use factory framework for generating test data for ActiveRecord models.

Setup Examples

KathyLee::User.define do
  username(fake(:user_name))
  email(fake(:email))
  home_page_url(fake(:url))
  fullname(fake(:full_name))
  (0)

  has_many :posts, :size => 2
  has_many :comments, :size => 1
end

KathyLee::User::Marge.define do
  fullname('Marge Simpson')
  (1000)
end

KathyLee::User::Marge::Other.define do
  home_page_url('http://www.simpsons.com')

  has_many :posts, :size => 5
  has_many :comments, :size => 1
end

KathyLee::Post.define do
  title(fake(:title))
  body(fake(:body))

  belongs_to :user
  has_many :comments, :size => 1
end

KathyLee::Comment.define do
  body(fake(:body))

  belongs_to :user
  belongs_to :post
end

KathyLee::Movie.define(:title => 'Rocky II', :director => 'Sly Stallone')

# Build a new unsaved model:
user = KathyLee::User.new
user.username.should == 'bill.smith'
user.email.should == '[email protected]'
user.home_page_url.should == 'http://www.example.com'
user.fullname.should == 'Bill Smith'
user..should == 0
user.new_record?.should be_true

# Build a new unsaved model with a few overrides:
user = KathyLee::User.new(:username => 'mark.bates', :login_count => 5)
user.username.should == 'mark.bates'
user.email.should == '[email protected]'
user.home_page_url.should == 'http://www.example.com'
user.fullname.should == 'Bill Smith'
user..should == 5
user.new_record?.should be_true

# Build and save a new model:
user = KathyLee::User.create
user.username.should == 'bill.smith'
user.email.should == '[email protected]'
user.home_page_url.should == 'http://www.example.com'
user.fullname.should == 'Bill Smith'
user..should == 0
user.new_record?.should be_false

# Build and save a new model with a few overrides:
user = KathyLee::User.create(:username => 'mark.bates')
user.username.should == 'mark.bates'
user.email.should == '[email protected]'
user.home_page_url.should == 'http://www.example.com'
user.fullname.should == 'Bill Smith'
user..should == 0
user.new_record?.should be_false

# Build many unsaved models (can be used with/without block):
users = KathyLee::User.sweatshop(10) do |user, index|
  user. = index
end
users.size.should == 10
users.each_with_index do |user, index|
  user..should == index
  user.new_record?.should be_true
end

# Build and save many models (can be used with/without block):
users = KathyLee::User.sweatshop!(10) do |user, index|
  user. = index
end
users.size.should == 10
users.each_with_index do |user, index|
  user..should == index
  user.new_record?.should be_false
end

# Build belongs_to associations (change KathyLee::Post.new to KathyLee::Post.create to save the whole thing):
post = KathyLee::Post.new
post.title.should_not be_nil
post.user.should_not be_nil
post.user.should be_kind_of(User)
post.user.email.should == '[email protected]'

# Build has_many associations (change KathyLee::User.new to KathyLee::User.create to save the whole thing):
user = KathyLee::User.new
user.should_not be_nil
user.posts.should_not be_nil
user.posts.size.should == 2
user.posts.each do |post|
  post.should be_kind_of(Post)
  post.title.should_not be_nil
end
user.comments.should_not be_nil
user.comments.size.should == 1
user.comments.each do |comment|
  comment.should be_kind_of(Comment)
end

# Build a variant of a model using subclasses:
user = KathyLee::User::Marge.new
user.username.should == 'bill.smith'
user.email.should == '[email protected]'
user.home_page_url.should == 'http://www.example.com'
user.fullname.should == 'Marge Simpson'
user..should == 1000
user.new_record?.should be_true
user.posts.size.should == 2

# Subclass your subclass for further customization:
user = KathyLee::User::Marge::Other.new
user.username.should == 'bill.smith'
user.email.should == '[email protected]'
user.home_page_url.should == 'http://www.simpsons.com'
user.fullname.should == 'Marge Simpson'
user..should == 1000
user.new_record?.should be_true
user.posts.size.should == 5

Contact

Please mail bugs, suggestions and patches to “[email protected]”:[email protected]

On the web at: “www.metabates.com”:www.metabates.