Created
August 14, 2014 08:59
-
-
Save kidach1/4d5d5b82f8b0e84dae43 to your computer and use it in GitHub Desktop.
[RSpec][FactoryGirl] クラスは異なるが名前が等しい属性をそれぞれsequenceする ref: http://qiita.com/kidachi_/items/212e5ffa7d2cd4937726
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sequence(name, *args, &block) | |
FactoryGirl.register_sequence(Sequence.new(name, *args, &block)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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