简体   繁体   中英

Run stored procedures in InMemory unit tests

How do I run stored procedures in an in-memory databases? I am trying to search for this functionality.

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

This is for setup:

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();
}

Short answer; the in-memory provider can't do it.

I had the same issue; I had stored procs surfacing in unit tests and I needed to mock the results. After searching for a library to do it and finding none that could do the big 3 ( FromSql , ExecuteSqlCommand and Queries) I wrote my own: EntityFrameworkCore.Testing . It uses the in-memory provider for most things and provides mocks for the bits it can't do.

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