K
K
Karina2015-04-18 18:33:44
Ruby on Rails
Karina, 2015-04-18 18:33:44

Can you help me understand the syntax?

Hello.
I'm watching FactoryGirl right now.
I don't understand the construction

FactoryGirl.define do
  factory :home do
  #потом заполняются поля и сохраняются
  end
end

and then they just write somewhere. I don’t understand - what kind of construction is this:
test = create (:home)
factory :home do
...
end

factory is a method or what is it?
And how can you write such a thing as a factory in the Factory itself ?
It seems that in the Factory itself this is implemented through yeild, but then why use this method if we do everything ourselves? I looked about DSL, but, it seems, is not particularly suitable.
Thanks everyone for the replies

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeiwan, 2015-04-18
@iKapex

factory is a method that is passed a :home parameter (in Ruby, as you know, parentheses can be omitted) and a block (everything between do and end). factory is called in a block, which is passed to FactoryGirl.define, where define is a method of the FactoryGirl class. And, yes, all this is the same DSL.
test = create (:home) - in fact, the full entry looks like FactoryGirl.create(:home). Just to avoid writing the FactoryGirl class all the time, its methods are included in the spec_helper.rb file: config.include FactoryGirl::Syntax::Methods
The topic is rather difficult to understand. How FactoryGirl works is very well written in the article https://robots.thoughtbot.com/writing-a-domain-spe... - your own FactoryGirl is written there with an explanation of all points.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question