Ways of unit testing data access layer

前端 未结 3 555
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 08:26

I have be trying to look for an effective way in unit testing my data access layer in C#. I\'m primary a Java developer and have only used C# for about 6 months, in the past

3条回答
  •  长情又很酷
    2020-12-28 09:09

    To test DataAccess Layer you will need a more complex structure.

    DataAccess Layer will call references from Repository objects. Repo object will call references from Entity Framework DbSets via UnitOfWork design pattern.

    DataAccess Layer (TOP)
    |
    UnitOfWork
    |
    Repository Pattern Classes
    |
    EF Context
    |
    Actual Database

    After you set structure, then you will mock repository classes. For example, items will be inserted to DB instead will go to mock object. Later you will assert against your mock object to see item inserted or not.

    Please take a look at Implementing the Repository and Unit of Work Patterns

提交回复
热议问题