I have a repository
public class StudentsPersonalDetailsRepository : IStudentPersonalDetailsRepository
{
private readonly StudentManagementSystemEntities _studentsDbContext;
private readonly ILogger _logger;
public StudentsPersonalDetailsRepository(StudentManagementSystemEntities context, ILogger<IStudentPersonalDetailsRepository> logger)
{
_studentsDbContext = context;
_logger = logger;
}
public IQueryable<StudentPersonalDetails> StudentPersonalDetails => _studentsDbContext.StudentPersonalDetails;
......
}
In my Service layer, I am having a service as
public class StudentsPersonalDetailsService:IStudentPersonalDetailsService
{
private readonly IStudentPersonalDetailsRepository _repository;
private readonly ILogger _logger;
public StudentsPersonalDetailsService(IStudentPersonalDetailsRepository studentPersonalDetailsRepository,ILogger<StudentsPersonalDetailsService> logger)
{
_repository = studentPersonalDetailsRepository;
_logger = logger;
}
......
......
public StudentModelResponse GetStudentById(int id)
{
Domain.Entities.StudentPersonalDetails obj = _repository.StudentPersonalDetails.
Where(i => i.RollNo == id)
.Select(i=>new Domain.Entities.StudentPersonalDetails {
RollNo=i.RollNo,
FirstName=i.FirstName,
LastName=i.LastName,
MailId=i.MailId,
MiddleName=i.MiddleName,
DateOfBirth=i.DateOfBirth,
GenderOfPerson=i.GenderOfPerson
}).FirstOrDefault();
StudentModel ob = StudentModel.Translator(obj);
return new StudentModelResponse { StudentModel=ob};
}
}
My Test code is
namespace StudentUnitTests
{
public class StudentServiceShould
{
[Theory]
[InlineData(1)]
public void AbleToRetrieveStudentById(int n)
{
var mock = new Mock<IStudentPersonalDetailsRepository>();
var logger = new Mock<ILogger<StudentsPersonalDetailsService>> ();
var ob = new StudentsPersonalDetailsService(mock.Object, logger.Object);
}
}
}
I need to write a unit test for GetStudentById() and check the values returned by the function.
Please help me to how to mock the service layer.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…