2011-05-31 7 views
8

जब पद 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(); 

उत्तर

6

यह वास्तव में अपेक्षित डिफ़ॉल्ट व्यवहार है। वहाँ उस के लिए कई कारण हैं, लेकिन मूल रूप से यह है कि करने पर निर्भर करता है जब आप एक IEnumerable<T> AutoFixture के लिए पूछना वास्तव में हद तक चला जाता है सुनिश्चित करने के लिए आप के लिए केवल क्या पूछना मिलता है।

यह कई लोगों के लिए आश्चर्य की बात व्यवहार है। अच्छी खबर यह है कि आप इसे बदल सकते हैं।

fixture.Customizations.Add(new StableFiniteSequenceRelay()); 

यह व्यवहार को बदल देगा जैसे कि बाद में सभी अनुक्रम स्थिर हैं। आप उस विधि कॉल को Customization for better reusability में पैकेज कर सकते हैं। ऐसा कुछ ऐसा दिखाई दे सकता है (पूरी तरह से वैकल्पिक):

public class StableFiniteSequenceCustomization : ICustomization 
{ 
    public void Customize(IFixture fixture) 
    { 
     fixture.Customizations.Add(new StableFiniteSequenceRelay()); 
    } 
} 
+0

एक बार फिर उत्कृष्ट सहायता! मैं इस सवाल का जवाब स्वीकार कर लिया है, लेकिन यह ++ हो सकता है अगर आप आप से क्या मतलब है पर विस्तार से बता सकता है "AutoFixture वास्तव में हद तक चला जाता है आप के लिए केवल क्या पूछना सुनिश्चित करने के लिए?" अर्थात्, मुझे शायद कुछ शिक्षा की आवश्यकता है जो मैं पूछ रहा हूं! :) – rbellamy

+2

गहरी व्याख्या के लिए एक संपूर्ण ब्लॉग पोस्ट की आवश्यकता होती है (जो * भविष्य में दिन की रोशनी देखेंगे)। संक्षिप्त जवाब यह है कि 'Inumerable 'का' अनुबंध 'केवल एक इटरेटर निर्दिष्ट करता है। यह एक सूची के समान नहीं है। अस्थिर इटरेटर या यहां तक ​​कि जनरेटर भी इंटरफेस फिट बैठते हैं। –

+1

इसलिए 'उपज' व्यवहार? – rbellamy

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