简体   繁体   English

使用自定义模型对WebAPI进行单元测试

[英]Unit testing WebAPI with a custom model

I have 2 tables in a database and structure looks like 我在数据库中有2个表,结构看起来像

Student [Id, Name , Email, Gender] 学生[身份证,姓名,电子邮件,性别]

Test [Id,StudentId,Name,Status] 测试[Id,StudentId,Name,Status]

UserSummary [Id, Name , Email, Gender,TestName,TestStatus] UserSummary [ID,名称,电子邮件,性别,TestName,TestStatus]

So in Unit testing approach which one should I validate? 那么在单元测试方法中,我应该验证哪一种? db model or custom model? 数据库模型还是自定义模型?

My API endpoint returns list of UserSummary as JSON back to front-end. 我的API端点将UserSummary列表作为JSON返回到前端。

[TestMethod]
public void GetAllStudents()
{
     IQueryable<Student> masterdata = new List<Student>
        {
            new Student {StudentID=1, Firstname = "AAA",Active_InActive=1 },
            new Student {StudentID=2, Firstname = "BBB" ,Active_InActive=1 },
            new Student {StudentID=3, Firstname = "ZZZ" ,Active_InActive=1 },
        }.AsQueryable();
    var mockSet = new Mock<DbSet<Student>>();
    mockSet.As<IQueryable<Student>>().Setup(m => m.Provider).Returns(masterdata.Provider);
    mockSet.As<IQueryable<Student>>().Setup(m => m.Expression).Returns(masterdata.Expression);
    mockSet.As<IQueryable<Student>>().Setup(m => m.ElementType).Returns(masterdata.ElementType);
    mockSet.As<IQueryable<Student>>().Setup(m => m.GetEnumerator()).Returns(masterdata.GetEnumerator());

    var mockContext = new Mock<SchoolDbEntities>();
    mockContext.Setup(c => c.Students).Returns(mockSet.Object);

    var service = new StudentDbHandler(mockContext.Object);
    var students = service.GetStudents(); //it returns list of Student 

    //So should i test Db models are same or Custom List<UserSummary> models are same?
}

So if I have to validate custom model, should I mock the custom model as well from the Moq entity framework data? 因此,如果必须验证自定义模型,是否还应该从Moq实体框架数据中模拟自定义模型? Can someone suggest any tutorials explains the way of handling custom model data other than Db models directly? 有人可以建议任何教程解释直接处理Db模型以外的自定义模型数据的方法吗?

If you begin testing the db model students results then all you are really testing is that the mocking library actually works. 如果您开始测试db模型students结果,那么您真正要测试的是模拟库确实有效。

You should be testing the call to GetStudents() and how it interacts with the mockContext . 您应该测试对GetStudents()的调用以及它如何与mockContext交互。 I don't know the internals of that method but I would suspect there are calls to a database and validation checks. 我不知道该方法的内部原理,但我怀疑存在对数据库的调用和验证检查。 You would assert against the mock context to see if the they were called as expected. 您可以针对模拟上下文进行断言,以查看是否按预期调用了它们。

You setup the data so checking the data doesn't make a valid test. 您设置数据,以便检查数据不会进行有效的测试。 You need to test the interaction with your code. 您需要测试与代码的交互。

You should integrate in your architecture a layer (DAO) for data access. 您应该在体系结构中集成一个用于访问数据的层(DAO)。 This layer will be in charge of persisting objects and executing queries. 该层将负责持久化对象和执行查询。

To test this layer you have no other choice than testing against a real DB (maybe an in memory mode). 要测试该层,除了对真实的DB(可能是处于内存模式)进行测试,别无选择。 This will allow you to validate your mappings and queries. 这将使您能够验证映射和查询。

The another layer (Business Layer) should rely on the previous DAO layer. 另一层(业务层)应依赖先​​前的DAO层。 To test the business layer, you may provide mocks for the DAO. 为了测试业务层,您可以为DAO提供模拟。 The you can test the business layer without database. 您可以在没有数据库的情况下测试业务层。

When possible, separate methods in your business layer between the ones which use DAO and the ones which don't. 如果可能,请在业务层中将使用DAO的方法与不使用DAO的方法分开。 It's easier to test a static method which takes entities in entry and provides entities in output, sometimes no mock are required. 测试静态方法更容易,该方法将实体作为输入项并在输出中提供实体,有时不需要模拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM