11

I'm using entity framework and trying to unit test my data services which are using EF. I'm not using repository and unit of work patterns. I tried the following approach to mock the context and 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);

    }

I get null "Object reference not set to an instance of an object" error for idbSet "Local". Is there any way to mock idbSet like this? Thanks

4 Answers 4

9

I worked it out like this: Created two classes named 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
}

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

Then tested like this:

[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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

You... need more reputation for this question/answer. I see that tons of people have this problem. I was hoping I wouldn't have to go this route, but you already did the heavy lifting :)
Sorry, but ... what interface is this: IEFModelMock?
2

You have to set up the Local property of your idbSet mock.


For example:

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

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

1 Comment

Thanks for your answer, but it didn't work, I finally mocked the IDbSet and IEFModel manually.
0

I had been using a method to create my mock sets like this:

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

I merely added this line:

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

before the return statement and it solved my problems.

Many of my queries look like this:

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

So I just need Local to not be null so that it doesn't throw a null reference exception.

Comments

0

I just solved the problem with a less intensive workaround: In-Memory-DB

step-by-step:

  1. add the https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.InMemory-nuget to your UnitTest-Project.

  2. instead of creating a Mock of the EF-context (MyContextMock = new Mock<MyContext>();), i created an InMemory-Instance:

// Create In-Memory-DB for tests involving the "local"-propertyvar InMemoryMockOptions = new DbContextOptionsBuilder<MyContext>();
InMemoryMockOptions.UseInMemoryDatabase("Mock");
MyContextInMemory = new MyContext(InMemoryMockOptions.Options);

// reset In-Memory DB for multiple runs.
MyContextInMemory.Database.EnsureDeleted();
MyContextInMemory.Database.EnsureCreated();
  1. Replace the Mock-setup with the InMemory-Instance in your UnitTests, like you would in code surrounding your UnitTest:
private void SetupEntities() // this is called in the Test initialization 
{
    var entitiesList = new List<MyEntity>
    {
        new MyEntity
        {
            Id = 1,
            Description = "Foo"
        },
        new MyEntity
        {
            Id = 2,
            Description = "Bar"
        },
        new MyEntity
        {
            Id = 3,
            Description = "FooBar"
        }
    };


    // var mockData = entitiesList .AsQueryable().BuildMockDbSet();
    // MyContextMock.Setup(mock => mock.TbBkv).Returns(mockData.Object);

    MyContextInMemory.TbBkv.AddRange(entitiesBkv);
    MyContextInMemory.SaveChanges();
}

This gives me a lot more freedom for testing. E.g. If you have a function preventing duplicates from a post-reqest, you can omit the "saveChanges"-call and have the context exactly in the state it would be when the tested-method is called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.