简体   繁体   English

Entity Framework Core FromSql 模拟测试用例

[英]Entity Framework Core FromSql mock test cases

I am using EF Core non-entity model with get stored procedure calling.我正在使用带有 get 存储过程调用的 EF Core 非实体模型。 See below sample code请参阅下面的示例代码

context.Query<ClassDTO>().FromSql("SpName @Param1, @Param2, @Param3", 
                                  new SqlParameter[] { param1, param2, param3 }).ToList();

Code is working fine.代码工作正常。 But I need to write mock test cases.但是我需要编写模拟测试用例。

Can anyone help me out?谁能帮我吗? How to mock Context.Query or how to write test cases for this code?如何模拟Context.Query或如何为此代码编写测试用例?

I tried to implement the follow way:我试图实现以下方式:

https://nodogmablog.bryanhogan.net/2017/11/unit-testing-entity-framework-core-stored-procedures/ https://nodogmablog.bryanhogan.net/2017/11/unit-testing-entity-framework-core-stored-procedures/

But it will work for ** productContext.Products.MockFromSql(...)但它适用于 ** productContext.Products.MockFromSql(...)

But for me It is Like productContext.Query.MockFromSql().但对我来说它就像 productContext.Query.MockFromSql()。 So advice me how to write test cases.所以建议我如何编写测试用例。

Thanks in advance.提前致谢。

A Siva湿婆

The actual FromSql mock set up for a DbQuery<TQuery> is the same as a DbSet<TEntity> so the OP link is relevant (although it's a catch all implementation, if you need to match on the FromSql sql/parameters you'll need to do additional mock set up; warning: it gets ugly quickly).DbQuery<TQuery>设置的实际 FromSql 模拟与DbSet<TEntity>相同,因此 OP 链接是相关的(尽管它是一个全面的实现,如果您需要匹配您需要的 FromSql sql/参数进行额外的模拟设置;警告:它很快就会变得丑陋)。

You need to mock the DbQuery<TQuery> with a queryable sequence, mock the query provider as per the OP link (extended with any specific mock matching that you need) and then assign the DbQuery mocked object to BOTH the DbContext .Query<TQuery>() method and the DbContext DbQuery<TQuery> property.您需要使用可查询序列模拟DbQuery<TQuery> ,根据 OP 链接模拟查询提供程序(扩展为您需要的任何特定模拟匹配),然后将 DbQuery 模拟对象分配给 DbContext .Query<TQuery>()方法和 DbContext DbQuery<TQuery>属性。

In terms of mocking a DbQuery, this is what I use to create one:在模拟 DbQuery 方面,这就是我用来创建的:

public static Mock<DbQuery<TQuery>> CreateDbQueryMock<TQuery>(this DbQuery<TQuery> dbQuery, IEnumerable<TQuery> sequence) where TQuery : class {
    var dbQueryMock = new Mock<DbQuery<TQuery>>();

    var queryableSequence = sequence.AsQueryable();

    dbQueryMock.As<IAsyncEnumerableAccessor<TQuery>>().Setup(m => m.AsyncEnumerable).Returns(queryableSequence.ToAsyncEnumerable);
    dbQueryMock.As<IQueryable<TQuery>>().Setup(m => m.ElementType).Returns(queryableSequence.ElementType);
    dbQueryMock.As<IQueryable<TQuery>>().Setup(m => m.Expression).Returns(queryableSequence.Expression);
    dbQueryMock.As<IEnumerable>().Setup(m => m.GetEnumerator()).Returns(queryableSequence.GetEnumerator());
    dbQueryMock.As<IEnumerable<TQuery>>().Setup(m => m.GetEnumerator()).Returns(queryableSequence.GetEnumerator()); 
    dbQueryMock.As<IQueryable<TQuery>>().Setup(m => m.Provider).Returns(queryableSequence.Provider);

    return dbQueryMock;
}

Then if I need to support FromSql I change the provider to a query provider mock (as per the OP the mock set up for CreateQuery):然后,如果我需要支持 FromSql,我将提供程序更改为查询提供程序模拟(根据 OP 为 CreateQuery 设置的模拟):

mock.As<IQueryable<TEntity>>().Setup(m => m.Provider).Returns(queryProviderMock.Object);

I ended up wrapping the above in a library if you want to save yourself some time: https://github.com/rgvlee/EntityFrameworkCore.Testing如果您想节省一些时间,我最终将上述内容包装在一个库中: https : //github.com/rgvlee/EntityFrameworkCore.Testing

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

相关问题 Entity Framework Core FromSqlRaw 模拟测试用例 - Entity Framework Core FromSqlRaw mock test cases 实体框架核心FromSQL引发ArgumentNullException - Entity Framework core FromSQL throws ArgumentNullException 用干净的EF Core查询替换Entity Framework FromSql() - Replace Entity Framework FromSql() with clean EF Core query 调用 FromSql 在 IQueryable 上不起作用<tentity>从 Entity Framework 核心 2.2 迁移到 3.0 之后</tentity> - Calling FromSql Not working on IQueryable<TEntity> after migration from Entity Framework core 2.2 to 3.0 使用FromSql方法(Linq)的实体框架核心输出参数在存储过程中不起作用 - Entity framework core Output parameter not working in store procedure using FromSql method (Linq) 实体框架7 FromSql存储过程返回值 - Entity Framework 7 FromSql stored procedure return value 如何模拟实体框架核心更改跟踪 - How to Mock Entity Framework Core Change Tracking 如何使用 Entity Framework Core 模拟异步存储库 - How to mock an async repository with Entity Framework Core 具有实体内存测试双重功能的模拟实体框架 - Mock Entity Framework with in Memory Test Double 实体框架:2.2 版本中将字符串参数传递给 FromSql 语句 - Entity Framework: Passing String Parameter to FromSql Statement in Version 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM