2012-07-18 11 views
6

मैं अपने कारखाने में के बाद निर्माण/बनाने हुक सूख करना चाहते हैं का निर्माण:सूखी FactoryGirl के बाद बनाने/हुक

FactoryGirl.define do 

    factory :poll do 

    sequence :title do |n| 
     "MyPollTitle#{n}" 
    end 
    sequence :description do |n| 
     "MyPollDescription#{n}" 
    end 
    user 

    factory :poll_with_answers do 

     ignore do 
     answers_count 2 
     end 

     after(:build) do |poll, evaluator| 
     evaluator.answers_count.times do 
      poll.answers << build(:answer, poll: poll) 
     end 
     end 

     after(:create) do |poll, evaluator| 
     evaluator.answers_count.times do 
      poll.answers << create(:answer, poll: poll) 
     end 
     end 
    end 
    end 
end 

समस्या मैं का सामना करना पड़ रहा था ऐसा लगता है कि मैं FG में तरीकों को परिभाषित नहीं कर सकते हैं? आइडिया इसे कैसे ड्रॉ करें?

उत्तर

6

सबसे पहले, after(:create) परोक्ष after(:build) कहता है, कम से कम FactoryGirl के हाल के संस्करणों में:

के बाद (: निर्माण) - (, FactoryGirl.create FactoryGirl.build के माध्यम से) के बाद एक कारखाने बनाया गया है कहा जाता है

https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#callbacks

तो आपके मामले में, निम्नलिखित कोड पर्याप्त होना चाहिए:

after(:build) do |poll, evaluator| 
    evaluator.answers_count.times do 
    poll.answers << build(:answer, poll: poll) 
    end 
end 

हालांकि, after(:build) ट्रिगर नहीं है जब आप build_stubbed() बजाय build() का उपयोग करें, जो है मुझे क्या करना है जब मैं इस सूत्र में आए कोशिश कर रहा था। इस कोड को सुखाने के लिए, यह पता चला है आप एक से अधिक तरीकों के लिए एक ही ब्लॉक कॉल करने के लिए callback() विधि का उपयोग कर सकते हैं:

factory :user do 
    callback(:after_build, :after_stub) do |user| 
    do_something_with(user) 
    end 
end 
1

यह एक सस्ते चाल हो सकता है, लेकिन आप दूसरे कारखाने में एक लैम्ब्डा बना सकते हैं:

factory :poll_with_answers do 
    ignore do 
    answers_count 2 
    end 

    make_answers = lambda do |poll, evaluator, method| 
    evaluator.answers_count.times do 
     poll.answers << send(method, :answer, poll: poll) 
    end 
    end 

    after(:build) do |poll, evaluator| 
    make_answers.call poll, evaluator, :build 
    end 

    after(:create) do |poll, evaluator| 
    make_answers.call poll, evaluator, :create 
    end 
end 

मैं इस पैटर्न के साथ खुश नहीं हूँ, लेकिन कम से कम यह सूखी के सामान ऊपर।

+0

यह चाल करना चाहिए। यह आपके कोड के बराबर है, इसलिए यदि मूल्यांकनकर्ता लैम्ब्डा संस्करण में शून्य है, तो यह आपके संस्करण में भी शून्य होना चाहिए। क्या आप मुझे मूल कार्य कोड और कोड जो कि नाइल्स (स्टैकट्रैक) प्राप्त करते हैं, दोनों को भी जोड़ सकते हैं। मेरे पास वर्तमान में डीबग करने के लिए पर्याप्त जानकारी नहीं है। –

+0

आपको इसमें कोई और काम निवेश करने की आवश्यकता नहीं है, इससे कोई फर्क नहीं पड़ता, मैं मूल रूप से सिर्फ यह जांचना चाहता था कि शॉर्टकट है या नहीं। वैसे भी मैंने https://gist.github.com/3140033 पर एक गिस्ट बनाया जहां आप मॉडल देख सकते हैं, शेष 1: 1 है। – wintersolutions

+0

खैर, कोई चिंता नहीं, लेकिन कोड को जांचने के बाद, मुझे नहीं पता कि 'मूल्यांकनकर्ता' क्या है :) –

संबंधित मुद्दे