微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 ASP.NET WebApi 中模拟类?

如何解决如何在 ASP.NET WebApi 中模拟类?

我需要模拟简单的类:

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}

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

解决方法

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

然后是这一行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 的类时,为 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 库。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?