2012-10-06 13 views
6

Kurucunun koleksiyon argümanına ihtiyacı olan soyut bir sınıfa sahibim. Test etmek için sınıfımla nasıl dalga geçebilirim? yapıcıya iletilen koleksiyondanOluşturucu bağımlılıkları olan (moq ile) özet sınıfı alaylı

public abstract class QuoteCollection<T> : IEnumerable<T> 
     where T : IDate 
    { 
     public QuoteCollection(IEnumerable<T> quotes) 
     { 
      //... 
     } 

     public DateTime From { get { ... } } 

     public DateTime To { get { ... } } 
    } 

Her öğenin uygulamalıdır:

public interface IDate 
{ 
    DateTime Date { get; } 
} 

benim özel bu şekilde görünecektir alay yazmak istiyorsanız:

public class QuoteCollectionMock : QuoteCollection<SomeIDateType> 
{ 
    public QuoteCollectionMock(IEnumerable<SomeIDateType> quotes) : base(quotes) { } 
} 

Ben MOQ ile bunu başarmak Can ?

cevap

11

Sen çizgisinde bir şeyler yapabilir: :) bir cazibe gibi

public abstract class AbstractClass 
{ 
    protected AbstractClass(int i) 
    { 
     this.Prop = i; 
    } 
    public int Prop { get; set; } 
} 
// ... 
    [Fact] 
    public void Test() 
    { 
     var ac = new Mock<AbstractClass>(MockBehavior.Loose, 10); 
     Assert.Equal(ac.Object.Prop, 10); 
    } 
+0

İşleri :) – Kuba

+0

sevindim yardımcı olabilir:

var myQuotes = GetYourQuotesIEnumerableFromSomewhere(); // the mock constructor gets the arguments for your classes' ctor var quoteCollectionMock = new Mock<QuoteCollection<YourIDate>>(MockBehavior.Loose, myQuotes); // .. setup quoteCollectionMock and assert as you please .. 

İşte çok basit bir örnek –