Shadi
Shadi

Reputation: 307

Unit testing add operation of a dbContext using Moq

I'm trying to start TDD in my application development,I'm using xunit an moq for mocking an unit testing the entityframework operations,I don't have any problem in testing update or delete operation, but unluckily because of not being experienced in this subject,I'm facing a problem in checking add method of a specific entity. Whenever I want to test the add method after executing SaveChanges() my context which was a list of mocked entities is set to null,so I cannot work with this context in my business code anymore. The link which I've used as a reference is the following:

https://msdn.microsoft.com/en-us/data/dn314429.aspx#virtual

Here is my Business code:

public class PersonBL
{
    /// <summary>
    /// DataContext item of this business.
    /// </summary>
    private UnitTestSampleEntities DataContext;

    /// <summary>
    /// Initializes a new instance of the <see cref="PersonBL"/> current business class for Person.
    /// </summary>
    /// <param name="dataContext">The data context.</param>
    public PersonBL()
    {
        this.DataContext = new UnitTestSampleEntities();
    }

    public PersonBL(UnitTestSampleEntities dbContext)
    {
        this.DataContext = dbContext;
    }


    /// <summary>
    /// Adds the new person.
    /// </summary>
    /// <param name="person">The person.</param>
    public void AddPerson(Person person)
    {

        this.DataContext.People.Add(person);

        this.DataContext.SaveChanges();
    }

    public void UpdatePerson(Person person)
    {
        var p = this.DataContext.People.Where(i => i.ID == person.ID).FirstOrDefault<Person>();

        if (p != null)
        {
            p.FirstName = person.FirstName;
            p.LastName = person.LastName;
            p.PhoneNo = person.PhoneNo;
        }

        this.DataContext.SaveChanges();
    }
    public void DeletePerson(Person person)
    {
        var p = this.DataContext.People.Where(i => i.ID == person.ID).FirstOrDefault<Person>();

        if (p != null)
        {
            this.DataContext.People.Remove(p);
        }

        this.DataContext.SaveChanges();

    }

    public List<Person> GetPeopleListByDeptID(int deptID)
    {
        return (from person in this.DataContext.People
                select person)
                .Where(i => i.DeptId == deptID)
                .ToList();

    }
}

and here is my test method:

 [Fact]
 public void TestAddPerson2()
 {
     //Prepare the data
     var data = new List<Person> 
     { 
         new Person { ID=1,
             FirstName="Joey", 
             LastName="Clark",
             PhoneNo="02188882525",
             DeptId=1}, 
         new Person {ID=2,
             FirstName="John",
             LastName="Peterson", 
             PhoneNo="02188884747",
             DeptId=1}, 
         new Person {ID=3,
             FirstName="Ross", 
             LastName="Geller",
             PhoneNo="02188883366",
             DeptId=1}, 
       }.AsQueryable();

     var mockSet = new Mock<DbSet<Person>>();

     mockSet.As<IQueryable<Person>>().Setup(m => m.Provider).Returns(data.Provider);
     mockSet.As<IQueryable<Person>>().Setup(m => m.Expression).Returns(data.Expression);
     mockSet.As<IQueryable<Person>>().Setup(m => m.ElementType).Returns(data.ElementType);
     mockSet.As<IQueryable<Person>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

     var mockContext = new Mock<UnitTestSampleEntities>();

     int addCount = 0;

     //setup the mock context
     // mockContext.Setup(m => m.People).Returns(mockSet.Object);
     mockContext.Setup(m => m.People).Callback(() => addCount++);

     // mockContext.Setup(m => m.Add(It.IsAny<Person>())).Callback(() => addCount++);
     var personBL = new PersonBL(mockContext.Object);

     var person = new Person()
     {
         FirstName = "John",
         LastName = "Clark",
         PhoneNo = "02188887788"
     };

     personBL.AddPerson(person);

     Assert.Equal(4, data.Count());

     mockSet.Verify(m => m.Add(It.IsAny<Person>()), Times.Once());
     mockContext.Verify(m => m.SaveChanges(), Times.Once());
 }

I'm looking for a way to check the count of records after saving a new record,or checking the value of a property of an entity and also a way to work with DataContext after add operation(why is it null?)

Upvotes: 1

Views: 3148

Answers (1)

Scott Nimrod
Scott Nimrod

Reputation: 11570

Use the Repository Pattern. And have a MockRepository for your unit test.

See my answer here:

How to write unit tests for data access?

Upvotes: 1

Related Questions