【问题标题】:How to mock class in ASP.NET WebApi?如何在 ASP.NET WebApi 中模拟类?
【发布时间】:2021-08-16 05:19:52
【问题描述】:

我需要模拟简单的类:

namespace Infrastructure.Repositores
{
    public class StudentRepository : IStudentRepository
    {
        private readonly GradeBookDBContext _dbContext;

        public StudentRepository(GradeBookDBContext dbContext)
        {
            _dbContext = dbContext;                
        }      

        public IEnumerable<Student> GetAllStudents()
        {
            
            var students = _dbContext.Students;
                                       
            return students;
        }

我尝试做这样的事情,但它不起作用......我是模拟测试的初学者,谁能解释我为什么它不起作用?

[Fact]
        public void Test2()
        {
            Mock<IStudentRepository> studentRepositoryMock =
                new Mock<IStudentRepository>();

            studentRepositoryMock.Setup(x => x.GetAllStudents())
                .Returns(SampleStudents);

            StudentRepository ss = new StudentRepository();




            var expected = SampleStudents();
            var actual = ss.GetAllStudents();

            Assert.Equal(expected, actual);
        }

        private List<Student> SampleStudents()
        {// list of sample objects}

如何模拟这个类保持良好的做法?

【问题讨论】:

标签: asp.net testing moq


【解决方案1】:

.Returns(SampleStudents) 必须是 .Returns(SampleStudents()),尽管除非 Studentstruct,否则 Assert.Equal(expected, actual) 仍会抛出异常

然后是StudentRepository ss = new StudentRepository();这一行。创建一个mock然后使用原始类是没有意义的

所以你应该这样做

[Fact]
public void Test1()
{
    Mock<IStudentRepository> studentRepositoryMock =
        new Mock<IStudentRepository>();
    var expected = Sample();

    studentRepositoryMock.Setup(x => x.GetAllStudents())
        .Returns(expected);

    var actual = studentRepositoryMock.Object.GetAllStudents();

    Assert.Equal(expected, actual);
}

显然这没有意义,因为您不会以这种方式测试任何有用的东西。如果您想测试 StudentRepository,只需创建 StudentRepository 并对其进行测试。当你有一个使用它的类时,为 IStudentRepository 创建模拟是有意义的,例如

public class StudentService
{
    private IStudentRepository _repo;

    public StudentService(IStudentRepository repo)
    {
        _repo = repo;
    }

    public List<Student> GetIgors()
    {
        return _repo.GetAllStudents().Where(x => x.Name == "Igor").ToList();
    }
}

那么你的测试应该是这样的

[Fact]
public void Test1()
{
    Mock<IStudentRepository> studentRepositoryMock =
        new Mock<IStudentRepository>();
    var expected = new List<Student>()
    {
        new Student() {Name = "Igor"},
        new Student() {Name = "NotIgor"}
    };

    studentRepositoryMock.Setup(x => x.GetAllStudents())
        .Returns(expected);

    
    var sm = new StudentService(studentRepositoryMock.Object);
    var actual = sm.GetIgors();

    Assert.True(actual.All(x => x.Name == "Igor"));
}

免责声明:我实际上并不建议您这样编写数据库查询,在将集合转换为IEnumerable 之前过滤元素,这只是使用moq 库的一个示例。如果您想测试存储库检查 this article ,请不要为此使用 moq 库。

【讨论】:

  • 你的意思是写查询,,这样,,?
  • 我的意思是过滤,这一行 _repo.GetAllStudents().Where(x => x.Name == "Igor").ToList();但是如果你在 IEnumerable 接口上执行 Where 方法,它会首先从数据库中获取所有数据,然后它会在应用程序端过滤列表。您可以在这篇文章中了解更多关于 IEnumerable 和 IQuerable 之间的区别thesharperdev.com/…
猜你喜欢
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2010-10-22
  • 2016-03-26
  • 1970-01-01
  • 2017-05-13
  • 2015-11-06
  • 1970-01-01
相关资源
最近更新 更多