Skip to content

Instantly share code, notes, and snippets.

@kidach1
Created August 14, 2014 08:59
Show Gist options
  • Save kidach1/4d5d5b82f8b0e84dae43 to your computer and use it in GitHub Desktop.
Save kidach1/4d5d5b82f8b0e84dae43 to your computer and use it in GitHub Desktop.
[RSpec][FactoryGirl] クラスは異なるが名前が等しい属性をそれぞれsequenceする ref: http://qiita.com/kidachi_/items/212e5ffa7d2cd4937726
module FactoryGirl
# @api private
class Configuration
attr_reader :factories, :sequences, :traits, :strategies, :callback_names
def initialize
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
@traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
@strategies = Registry.new('Strategy')
@callback_names = Set.new
@definition = Definition.new
to_create { |instance| instance.save! }
initialize_with { new }
end
~
end
def sequence(name, *args, &block)
FactoryGirl.register_sequence(Sequence.new(name, *args, &block))
end
module FactoryGirl
class Decorator
class DisallowsDuplicatesRegistry < Decorator
def register(name, item)
if registered?(name)
raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}"
else
@component.register(name, item)
end
end
end
end
end
sequence(:categories) { |n| "test_category_#{n}" }
factory :category do
name {generate(:categories)}
~
end
sequence(:tags) { |n| "test_tag_#{n}" }
factory :tag do
name {generate(:tags)}
~
end
$ spring rspec spec/hoge_spec.rb
/Users/kidachi_/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/decorator.rb:10:in `method_missing': Sequence already registered: name (FactoryGirl::DuplicateDefinitionError)
$ spring rspec spec/hoge_spec.rb
/Users/kidachi_/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/decorator.rb:10:in `method_missing': Sequence already registered: name (FactoryGirl::DuplicateDefinitionError)
# Generates and returns the next value in a sequence.
#
# Arguments:
# name: (Symbol)
# The name of the sequence that a value should be generated for.
#
# Returns:
# The next value in the sequence. (Object)
def generate(name)
FactoryGirl.sequence_by_name(name).next
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment