A Lazy Usage for Factory Girl

Factory girl let us product model instance feel luck. But for me(a lazy boy). I wanna write shorter that standard way.

So a… Look code below. I’m lazy to explain.

spec/factories.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class F
class << self
{:a => :attributes_for, :b => :build, :c => :create, :d => :define, :s => :stub, :* => :sequence, :+ => :next}.each do |k,v|
delegate v, :to => Factory
alias_method k, v
end
end
end

F.d :user do |u|
u.name "foo"
u.full_name "福娃"
u.password "0"
end

F.* :foos do |n|
"foo_#{n}"
end
spec/models/user_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require "spec_helper"

describe User do
after do
User.delete_all
end

it "should valid" do
F.b(:user).should be_valid
end

it "should save success" do
F.c(:user).should_not be_new_record
User.all.should be_one
end

it "should product some foos" do
F.+(:foos).should eql "foo_1"
(F + :foos).should eql "foo_2"
end
end