@manhnguyenv

Welcome to my blog!

Mock Object in Unit Testing

October 27, 2017 18:51

Mock Object in Unit Testing

Mock null object

tableRepositoryMock.Setup(m => m.FirstOrDefault(id)).Returns((AuditType)null); 

Mock InsertAndGetIdAsync

Guid id = Guid.NewGuid();
var auditLocation = new AuditLocation()
{
Id = id,
ColumnName = "Block",
ColumnOrder = 1
};
auditLocationRepositoryMock.Setup(m => m.InsertAndGetIdAsync(auditLocation)).Returns(Task.FromResult(id));

Mock FirstOrDefault()

var id = Guid.NewGuid();
var auditType = new AuditType
{
Id = id
};
auditTypeRepositoryMock.Setup(m => m.FirstOrDefault(id)).Returns(auditType);

Mock GetAll()

const string title = "Test 1";
var id = Guid.NewGuid();
var returnList = new List<AuditType>()
{
new AuditType()
{
Id = id,
Name = title
},
new AuditType()
{
Id = Guid.NewGuid(),
Name = "Test 2"
}
};

auditTypeRepositoryMock.Setup(m => m.GetAll()).Returns(returnList.AsQueryable());

Categories

Recent posts