Moq,使用xUnit框架进行单元测试并测试返回对象的函数

编程入门 行业动态 更新时间:2024-10-23 17:32:25
本文介绍了Moq,使用xUnit框架进行单元测试并测试返回对象的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个仓库

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框架进行单元测试并测试返回对象的函数

    本文发布于:2023-11-08 14:11:22,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1569592.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:函数   框架   单元测试   对象   测试

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!