Permalink
Browse files

Added a Sham syntax layer

  • Loading branch information...
jferris committed Feb 17, 2009
1 parent 47b6ca0 commit 03b3f9200f618148a8510d5abb44178cd65ad98a
Showing with 80 additions and 0 deletions.
  1. +40 −0 lib/factory_girl/syntax/sham.rb
  2. +40 −0 test/syntax/sham.rb
@@ -0,0 +1,40 @@
class Factory
module Syntax #:nodoc:
# Adds a Sham module, which provides an alternate interface to
# Factory::Sequence.
#
# Usage:
#
# require 'factory_girl/syntax/sham'
#
# Sham.email {|n| "somebody#{n}@example.com" }
#
# Factory.define :user do |factory|
# factory.email { Sham.email }
# end
#
# Note that you can also use Faker, but it is recommended that you simply
# use a sequence as in the above example, as factory_girl does not provide
# protection against duplication in randomized sequences, and a randomized
# value does not provide any tangible benefits over an ascending sequence.
module Sham
module Sham #:nodoc:
def self.method_missing(name, &block)
if block_given?
Factory.sequence(name, &block)
else
Factory.next(name)
end
end
# overrides name on Module
def self.name(&block)
method_missing('name', &block)
end
end
end
end
end
include Factory::Syntax::Sham
View
@@ -0,0 +1,40 @@
require 'test_helper'
require 'factory_girl/syntax/sham'
class ShamSyntaxTest < Test::Unit::TestCase
context "a factory" do
setup do
Sham.name { "Name" }
Sham.email { "somebody#{rand(5)}@example.com" }
Factory.define :user do |factory|
factory.first_name { Sham.name }
factory.last_name { Sham.name }
factory.email { Sham.email }
end
end
teardown do
Factory.factories.clear
Factory.sequences.clear
end
context "after making an instance" do
setup do
@instance = Factory(:user, :last_name => 'Rye')
end
should "support a sham called 'name'" do
assert_equal 'Name', @instance.first_name
end
should "use the sham for the email" do
assert_match /somebody\d@example.com/, @instance.email
end
end
end
end

0 comments on commit 03b3f92

Please sign in to comment.