Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

asp.net core mvc - Moq, unit test using xUnit framework and testing a function returning an object

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the above we have two things happening within StudentsPersonalDetailsService.GetStudentById()

  1. Retrieve the student info from the repository.
  2. Create a student model from the data retrieved from the repository

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);
     }
 }

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...