簡體   English   中英

使用moq進行單元測試實體框架

[英]Unit test Entity Framework using moq

我正在使用實體框架並嘗試對使用EF的數據服務進行單元測試。 我沒有使用存儲庫和工作單元模式。 我嘗試了以下方法來模擬上下文和DbSet:

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“Local”,我得到null“對象引用未設置為對象的實例”錯誤。 有沒有辦法像這樣模擬idbSet? 謝謝

我這樣做了:創建了兩個名為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
}

然后像這樣測試:

[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);
    }

您必須設置idbSet mock的Local屬性。


例如:

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

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

我一直在使用一種方法來創建我的模擬集:

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;
}

我只是添加了這一行:

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);

所以我只需要Local不為null,這樣它就不會拋出空引用異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM