我有一个仓库
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}; } }我的测试代码是
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); } } }我需要为GetStudentById()编写单元测试,并检查该函数返回的值.
I need to write a unit test for GetStudentById() and check the values returned by the function.
请帮助我模拟服务层.
推荐答案在上面,我们在StudentsPersonalDetailsService.GetStudentById()
注意:从存储库中读取时,有些东西看起来很奇怪.如果存储库中的项目为StudentPersonalDetails,为什么要创建新实例
Note: Something looks strange when reading from the repository. If the items in the repository are StudentPersonalDetails why create new instances
我们可以像这样存根检索学生数据
We can stub retrieving the student data like so
public class StudentServiceShould { [Theory] [InlineData(1)] public void AbleToRetrieveStudentById(int n) { var students = new []{ // new Domain.Entities.StudentPersonalDetails for student role 1, // new Domain.Entities.StudentPersonalDetails for student role 2, // new Domain.Entities.StudentPersonalDetails for student role 3 }; var mock = new Mock<IStudentPersonalDetailsRepository>(); mock.SetupGet(mk => mk.StudentPersonalDetails).Returns(students.AsQueryable()); var logger = new Mock<ILogger<StudentsPersonalDetailsService>> (); var ob = new StudentsPersonalDetailsService(mock.Object, logger.Object); } }创建StudentModel对象的过程封装在Translator中,但是由于它是'StudentModel'上的静态方法,因此我们无法对其进行模拟,而必须一次性测试读取和转换.
Creating the StudentModel objects is encapsulated in the Translator but because it is a static method on the 'StudentModel' we cannot mock it and will have to test the reading and conversion in one go.
更多推荐
Moq,使用xUnit框架进行单元测试并测试返回对象的函数
发布评论