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 dont' 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.
Here is my Business code: public class PersonBL { /// /// DataContext item of this business. /// 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,and also a way to work with DataContext after add operation(why it is null????)
I appreciate your care and help in advance
Aucun commentaire:
Enregistrer un commentaire