खोज…


कारखानों को परिभाषित करना

यदि आपके पास नाम और ईमेल विशेषताओं के साथ एक ActiveRecord उपयोगकर्ता वर्ग है, तो आप FactoryGirl का अनुमान लगाकर इसके लिए एक कारखाना बना सकते हैं:

FactoryGirl.define do
  factory :user do # it will guess the User class
    name     "John"
    email    "[email protected]"
  end
end

या आप इसे स्पष्ट कर सकते हैं और इसका नाम भी बदल सकते हैं:

FactoryGirl.define do
  factory :user_jack, class: User do
    name     "Jack"
    email    "[email protected]"
  end
end

फिर अपने अनुमान में आप इन के साथ FactoryGirl के तरीकों का उपयोग कर सकते हैं, जैसे:

# To create a non saved instance of the User class filled with John's data 
build(:user) 
# and to create a non saved instance of the User class filled with Jack's data
build(:user_jack)

सबसे आम तरीके हैं:

# Build returns a non saved instance
user = build(:user)

# Create returns a saved instance
user = create(:user)

# Attributes_for returns a hash of the attributes used to build an instance
attrs = attributes_for(:user)


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow