简体   繁体   English

如果使用存储过程,则对Entity Framework Core 2.0进行单元测试

[英]Unit testing Entity Framework Core 2.0 if using a stored procedure

I have a problem with unit testing data access layer. 我有单元测试数据访问层的问题。 I query the database via stored procedures, so now I can't use Entity Framework in-memory database because in-memory database is not a relational database. 我通过存储过程查询数据库,所以现在我不能使用Entity Framework内存数据库,因为内存数据库不是关系数据库。 Also, I read that I have to use integration tests but I can't find any example with testing using stored procedure. 另外,我读到我必须使用集成测试,但我找不到任何使用存储过程进行测试的示例。

So my question is how to test data access layer, do I need to create a test database that is same as my real database? 所以我的问题是如何测试数据访问层,我是否需要创建一个与我的真实数据库相同的测试数据库?

This is my code for data access layer: 这是我的数据访问层代码:

public class Repository<TEntity> : IRepository<TEntity> where TEntity : Domains.Users
{
    private DbSet<TEntity> dbSet;

    private readonly DbContext context;

    public Repository(DbContext context)
    {
        this.context = context;
        dbSet = context.Set<TEntity>();
    }

    public void Add(TEntity entity)
    {
        if (entity is Domains.Users)
        {
            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@Adress";
            param1.DbType = System.Data.DbType.String;
            param1.Value = (entity as Domains.Users).Adress;

            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@Email";
            param2.DbType = System.Data.DbType.String;
            param2.Value = (entity as Domains.Users).Email;

            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@Name";
            param3.DbType = System.Data.DbType.String;
            param3.Value = (entity as Domains.Users).Name;

            SqlParameter param4 = new SqlParameter();
            param4.ParameterName = "@Lastname";
            param4.DbType = System.Data.DbType.String;
            param4.Value = (entity as Domains.Users).Lastname;

            context.Database.ExecuteSqlCommand("sp_AddUser @Adress, @Email, @Name, @Surname", param1, param2, param3, param4);
        }
    }
}

My advice is to use test ( real ) database (depending on the policies in your firm it can be database on a test server or you can use local instance of the database provider on your dev machine .. etc). 我的建议是使用测试( 真实 )数据库(根据您公司的策略,它可以是测试服务器上的数据库,或者您可以在您的开发机器上使用数据库提供程序的本地实例等)。

The point of the integration tests are to verify that your application is working stable as single unit. 集成测试的要点是验证您的应用程序是否作为单个单元稳定运行。 The test env. 测试环境 must be as close as possible to the production evn. 必须尽可能接近生产evn。 which means a real database . 这意味着一个真正的数据库 EF in memory database representation is no good for that. 内存数据库表示中的EF对此没有好处。

You can check also component tests . 您还可以检查组件测试 They are like the integration tests, but with them you don't test the application as a unit, but the major components of the application like repositories, services .. etc, on the other side with integration test you will most like test the REST api endpoints(in the context of REST application). 它们就像集成测试一样,但是使用它们你不会将应用程序作为一个单元进行测试,而是应用程序的主要组件,如存储库,服务......等,另一方面进行集成测试,你最喜欢测试REST api端点(在REST应用程序的上下文中)。

Some notes on integration/component tests 关于集成/组件测试的一些注意事项

  • In the best case the only thing you will need to change for the test env (your integration tests project). 在最好的情况下,您需要更改测试环境(您的集成测试项目)。 will be the connection string to use the local / test database configuration in the web/app.cofing. 将是在web / app.cofing中使用本地/测试数据库配置的连接字符串。
  • No mocks when using integration tests 使用集成测试时没有模拟
  • Reset the state of the database before each test. 在每次测试之前重置数据库的状态。 This way you can avoid your database grow in size. 这样可以避免数据库的大小增加。

Examples for integration tests 集成测试的示例

If you are developing REST api (just example). 如果您正在开发REST api(仅举例)。 This are very basic test case examples which are not to be taken as 100% best practices. 这是非常基本的测试用例,不能作为100%最佳实践。 Just to have a glance of what they could be. 只是想看看它们可能是什么。

  • Test the GET end points with records / no records in the database 使用数据库中的记录/无记录测试GET端点
  • Test the authorization policies for the different resources 测试不同资源的授权策略
  • Test the POST end points with valid / invalid data 使用有效/无效数据测试POST端点
  • Assert the database state, like table counts and the http respose code of the request 断言数据库状态,如表计数和请求的http respose代码
  • etc ... 等......

NOTE : In some cases we use the in memory representation only in the begging of the application for some prototyping before we start to develop the actual database. 注意 :在某些情况下,在开始开发实际数据库之前,我们仅在应用程序的请求中使用内存表示形式进行某些原型设计。

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

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