2012-10-30 3 views
11

का उपयोग कर यूनिट परीक्षण इकाई फ्रेमवर्क मैं इकाई ढांचे का उपयोग कर रहा हूं और ईएफ का उपयोग कर रहे मेरी डेटा सेवाओं का परीक्षण करने की कोशिश कर रहा हूं। मैं रिपोजिटरी और कार्य पैटर्न की इकाई का उपयोग नहीं कर रहा हूं।एमओसी

private static Mock<IEFModel> context; 
private static Mock<IDbSet<CountryCode>> idbSet; 

    [ClassInitialize] 
    public static void Initialize(TestContext testContext) 
    { 
     context = new Mock<IEFModel>(); 

     idbSet = new Mock<IDbSet<CountryCode>>(); 

     context.Setup(c => c.CountryCodes).Returns(idbSet.Object); 

    } 

मैं अशक्त मिल idbSet "स्थानीय" के लिए त्रुटि "ऑब्जेक्ट संदर्भ एक वस्तु का एक उदाहरण के लिए सेट नहीं": मैं संदर्भ और DbSet उपहास करने के लिए निम्नलिखित दृष्टिकोण की कोशिश की। क्या इस तरह idbSet नकली करने का कोई तरीका है? धन्यवाद

उत्तर

9

मैं इसे इस तरह बाहर काम किया वें की तरह है:

public static Mock<IDbSet<T>> CreateMockSet<T>(IQueryable<T> data) where T : class 
{ 
    var mockSet = new Mock<IDbSet<T>>(); 
    mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider); 
    mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression); 
    mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType); 
    mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); 
    return mockSet; 
} 

मैं केवल इस लाइन कहा::

[TestClass] 
public class CountryCodeServiceTest 
{ 
    #region Static Fields 

    /// <summary>The context.</summary> 
    private static IEFModel context; 

    #endregion 

    #region Public Methods and Operators 

    /// <summary>The initialize.</summary> 
    /// <param name="testContext">The test context.</param> 
    [ClassInitialize] 
    public static void Initialize(TestContext testContext) 
    { 
     context = new EFModelMock(); 
    } 

    /// <summary>The country code service get country codes returns correct data.</summary> 
    [TestMethod] 
    public void CountryCodeServiceGetCountryCodesReturnsCorrectData() 
    { 
     // Arrange 
     var target = new CountryCodeService(context); 
     var countryName = "Australia"; 
     var expected = context.CountryCodes.ToList(); 

     // Act 
     var actual = target.GetCountryCodes(); 

     // Assert 
     Assert.IsNotNull(actual); 
     Assert.AreEqual(actual.FirstOrDefault(a => a.CountryName == countryName).PhoneCode, expected.FirstOrDefault(a => a.CountryName == countryName).TelephoneCode); 
    } 
+1

आपको ... इस प्रश्न/उत्तर के लिए अधिक प्रतिष्ठा की आवश्यकता है। मैं देखता हूं कि बहुत से लोगों को यह समस्या है। मैं उम्मीद कर रहा था कि मुझे इस मार्ग पर जाना पड़ेगा, लेकिन आपने पहले ही भारी भारोत्तोलन किया है :) – vbullinger

+0

क्षमा करें, लेकिन ... यह इंटरफ़ेस क्या है: IEFModelMock? – GustavoAdolfo

2

आपको अपने idbSet नकली की Local संपत्ति सेट अप करना होगा।


उदाहरण के लिए: निर्मित दो वर्गों में नामित किया DbSetMock:

public class DbSetMock<T> : IDbSet<T> 
    where T : class 
{ 
    #region Fields 

    /// <summary>The _container.</summary> 
    private readonly IList<T> _container = new List<T>(); 

    #endregion 

    #region Public Properties 

    /// <summary>Gets the element type.</summary> 
    public Type ElementType 
    { 
     get 
     { 
      return typeof(T); 
     } 
    } 

    /// <summary>Gets the expression.</summary> 
    public Expression Expression 
    { 
     get 
     { 
      return this._container.AsQueryable().Expression; 
     } 
    } 

    /// <summary>Gets the local.</summary> 
    public ObservableCollection<T> Local 
    { 
     get 
     { 
      return new ObservableCollection<T>(this._container); 
     } 
    } 

    /// <summary>Gets the provider.</summary> 
    public IQueryProvider Provider 
    { 
     get 
     { 
      return this._container.AsQueryable().Provider; 
     } 
    } 

    #endregion 

    #region Public Methods and Operators 

    /// <summary>The add.</summary> 
    /// <param name="entity">The entity.</param> 
    /// <returns>The <see cref="T"/>.</returns> 
    public T Add(T entity) 
    { 
     this._container.Add(entity); 
     return entity; 
    } 

    /// <summary>The attach.</summary> 
    /// <param name="entity">The entity.</param> 
    /// <returns>The <see cref="T"/>.</returns> 
    public T Attach(T entity) 
    { 
     this._container.Add(entity); 
     return entity; 
    } 

    /// <summary>The create.</summary> 
    /// <typeparam name="TDerivedEntity"></typeparam> 
    /// <returns>The <see cref="TDerivedEntity"/>.</returns> 
    /// <exception cref="NotImplementedException"></exception> 
    public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T 
    { 
     throw new NotImplementedException(); 
    } 

    /// <summary>The create.</summary> 
    /// <returns>The <see cref="T"/>.</returns> 
    /// <exception cref="NotImplementedException"></exception> 
    public T Create() 
    { 
     throw new NotImplementedException(); 
    } 

    /// <summary>The find.</summary> 
    /// <param name="keyValues">The key values.</param> 
    /// <returns>The <see cref="T"/>.</returns> 
    /// <exception cref="NotImplementedException"></exception> 
    public T Find(params object[] keyValues) 
    { 
     throw new NotImplementedException(); 
    } 

    /// <summary>The get enumerator.</summary> 
    /// <returns>The <see cref="IEnumerator"/>.</returns> 
    public IEnumerator<T> GetEnumerator() 
    { 
     return this._container.GetEnumerator(); 
    } 

    /// <summary>The remove.</summary> 
    /// <param name="entity">The entity.</param> 
    /// <returns>The <see cref="T"/>.</returns> 
    public T Remove(T entity) 
    { 
     this._container.Remove(entity); 
     return entity; 
    } 

    #endregion 

    #region Explicit Interface Methods 

    /// <summary>The get enumerator.</summary> 
    /// <returns>The <see cref="IEnumerator"/>.</returns> 
    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return this._container.GetEnumerator(); 
    } 

    #endregion 
} 

और EFModelMock:

public class EFModelMock : IEFModel 
{ 
    #region Fields 

    /// <summary>The country codes.</summary> 
    private IDbSet<CountryCode> countryCodes; 

    #endregion 

    #region Public Properties 

    /// <summary>Gets the country codes.</summary> 
    public IDbSet<CountryCode> CountryCodes 
    { 
     get 
     { 
      this.CreateCountryCodes(); 
      return this.countryCodes; 
     } 
    } 


    #endregion 

    #region Public Methods and Operators 

    /// <summary>The commit.</summary> 
    /// <exception cref="NotImplementedException"></exception> 
    public void Commit() 
    { 
     throw new NotImplementedException(); 
    } 

    /// <summary>The set.</summary> 
    /// <typeparam name="T"></typeparam> 
    /// <returns>The <see cref="IDbSet"/>.</returns> 
    /// <exception cref="NotImplementedException"></exception> 
    public IDbSet<T> Set<T>() where T : class 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 

    #region Methods 

    /// <summary>The create country codes.</summary> 
    private void CreateCountryCodes() 
    { 
     if (this.countryCodes == null) 
     { 
      this.countryCodes = new DbSetMock<CountryCode>(); 
      this.countryCodes.Add(
       new CountryCode { CountryName = "Australia", DisplayLevel = 2,  TelephoneCode = "61" }); 

     } 
    } 

    #endregion 
} 

फिर परीक्षण किया

idbSet = new Mock<IDbSet<CountryCode>>(); 

var col = new ObservableCollection<CountryCode>(); 
idbSet.SetupGet(x => x.Local).Returns(col); 
+1

आपके उत्तर के लिए धन्यवाद, लेकिन यह काम नहीं किया, अंत में मैंने आईडीबीसेट और आईईएफ मॉडेल को मैन्युअल रूप से मजाक कर दिया। – Khash

0

मैं इस तरह मेरी नकली सेट बनाने के लिए एक विधि का उपयोग किया गया था वापसी कथन से पहले

mockSet.Setup(x => x.Local).Returns(new ObservableCollection<T>()); 

और इसे हल मेरी समस्याएं मेरे प्रश्नों के

कई इस तरह दिखेगा:

var myset = context.EntitySetName.Local.SingleOrDefault(x=>x.something==something) 
      ?? 
      context.SingleOrDefault(x=>x.something==something); 

तो मैं सिर्फ रिक्त नहीं हो करने के लिए स्थानीय जरूरत है इतना है कि यह एक अशक्त संदर्भ अपवाद फेंक नहीं है।

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