user11156893
user11156893

Reputation:

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://learn.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();
}

Upvotes: 3

Views: 8287

Answers (1)

rgvlee
rgvlee

Reputation: 3193

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.

Upvotes: 3

Related Questions