简体   繁体   中英

Unit testing WebAPI with a custom model

I have 2 tables in a database and structure looks like

Student [Id, Name , Email, Gender]

Test [Id,StudentId,Name,Status]

UserSummary [Id, Name , Email, Gender,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.

[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? Can someone suggest any tutorials explains the way of handling custom model data other than Db models directly?

If you begin testing the db model students results then all you are really testing is that the mocking library actually works.

You should be testing the call to GetStudents() and how it interacts with the 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. 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). This will allow you to validate your mappings and queries.

The another layer (Business Layer) should rely on the previous DAO layer. To test the business layer, you may provide mocks for the 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. It's easier to test a static method which takes entities in entry and provides entities in output, sometimes no mock are required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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