【问题标题】:Run stored procedures in InMemory unit tests在 InMemory 单元测试中运行存储过程
【发布时间】:2019-08-11 11:40:50
【问题描述】:

如何在内存数据库中运行存储过程?我正在尝试搜索此功能。

https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory

这是为了设置:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFProviders.InMemory;Trusted_Connection=True;ConnectRetryCount=0");
    }
}

var options = new DbContextOptionsBuilder<BloggingContext>()
                .UseInMemoryDatabase(databaseName: "Find_searches_url")
                .Options;

// Insert seed data into the database using one instance of the context
using (var context = new BloggingContext(options))
{
    context.Blogs.Add(new Blog { Url = "http://sample.com/cats" });
    context.Blogs.Add(new Blog { Url = "http://sample.com/catfish" });
    context.Blogs.Add(new Blog { Url = "http://sample.com/dogs" });

    context.SaveChanges();
}

【问题讨论】:

  • 底线是 SProcs 是服务器的东西,而不是 EF 的东西。您可以通过 C# 中的集成测试或直接在服务器上使用集成测试来测试存储过程。
  • 试图设置多个内存数据库项目,所以我没有在物理位置进行测试,我们需要为我们的测试运行多个拆卸和设置种子操作
  • 您必须意识到实体框架不是 SQL Server。它是一个可以与许多不同的数据库后端进行通信的框架。有MS-SQL、MySql,或者你已经找到了InMemory。您的代码被翻译成后端需要的任何“语言”。 SProcs 与 EF 无关。
  • 无论如何,您不应该使用存储过程。在代码中进行测试设置。如果您需要共享它,您可以创建一个帮助类或在 xUnit 中使用 IClassFixture 之类的东西。

标签: c# entity-framework asp.net-core .net-core .net-core-2.0


【解决方案1】:

简答;内存提供者无法做到这一点。

我有同样的问题;我在单元测试中存储了 procs 表面处理,我需要模拟结果。在搜索了一个库来做这件事并发现没有一个可以做大 3(FromSqlExecuteSqlCommand 和查询)之后,我写了自己的:EntityFrameworkCore.Testing。它对大多数事情使用内存提供程序,并为它不能做的部分提供模拟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2014-01-12
    • 2023-04-03
    • 1970-01-01
    • 2010-11-04
    • 2015-04-29
    • 1970-01-01
    相关资源
    最近更新 更多