जब पद here को देखते हुए ऐसा लगता है कि मैं, CreateMany()
का उपयोग कर कई वस्तुओं को बनाने के लिए उन्हें foreach
का उपयोग करने पर पुनरावृति, और फिर उन्हें एक सरणी के रूप में वापस जाने के लिए सक्षम होना चाहिए।AutoFixture IEnumerable <T> CreateMany साथ व्यवहार()
क्या मैं दिखाई दे रही है प्रत्येक यात्रा नई वस्तुओं हर बार बनाने के लिए लगता है कि है। क्या यह अपेक्षित व्यवहार है?
इकाई बनाने के लिए:
public class TestEntity
{
public int Id { get; private set; }
public string SomeString { get; set; }
public void SetId(int value)
{
this.Id = value;
}
}
नमूना Program.cs:
private static int id;
static void Main(string[] args)
{
var fixture = new Fixture();
IEnumerable<TestEntity> testEntities =
fixture.Build<TestEntity>().CreateMany(5);
Output(testEntities);
foreach (var testEntity in testEntities)
{
testEntity.SetId(id++);
Console.WriteLine(
string.Format("CHANGED IN FOREACH:: hash: {0}, id: {1}, string: {2}",
testEntity.GetHashCode(), testEntity.Id, testEntity.SomeString));
}
Output(testEntities);
}
private static void Output(IEnumerable<TestEntity> testEntities)
{
foreach (var testEntity in testEntities)
{
Console.WriteLine(
string.Format("hash: {0}, id: {1}, string: {2}",
testEntity.GetHashCode(), testEntity.Id, testEntity.SomeString));
}
}
मैं एक मुद्दा here (जो शायद इस व्यवहार की उम्मीद है, तो हटाया जा सकता है) बनाया।
संपादित 2011-06-02
व्यवहार मैं उम्मीद कर रहा था प्राप्त करने के लिए, और अगर मैं AutoFixture व्यवहार को संशोधित नहीं करना चाहता, मैं एक विस्तार विधि का उपयोग कर सकते हैं:
var fixture = new Fixture();
TestEntity[] testEntities = fixture.Build<TestEntity>().CreateMany(5).ToArray();
एक बार फिर उत्कृष्ट सहायता! मैं इस सवाल का जवाब स्वीकार कर लिया है, लेकिन यह ++ हो सकता है अगर आप आप से क्या मतलब है पर विस्तार से बता सकता है "AutoFixture वास्तव में हद तक चला जाता है आप के लिए केवल क्या पूछना सुनिश्चित करने के लिए?" अर्थात्, मुझे शायद कुछ शिक्षा की आवश्यकता है जो मैं पूछ रहा हूं! :) – rbellamy
गहरी व्याख्या के लिए एक संपूर्ण ब्लॉग पोस्ट की आवश्यकता होती है (जो * भविष्य में दिन की रोशनी देखेंगे)। संक्षिप्त जवाब यह है कि 'Inumerable 'का' अनुबंध 'केवल एक इटरेटर निर्दिष्ट करता है। यह एक सूची के समान नहीं है। अस्थिर इटरेटर या यहां तक कि जनरेटर भी इंटरफेस फिट बैठते हैं। –
इसलिए 'उपज' व्यवहार? – rbellamy