具有内存和依赖注入功能的EF Core单元测试?

时间:2018-10-09 10:14:09

标签: c# unit-testing .net-core entity-framework-core .net-core-2.1

一直在尝试通过.Net Dependency注入来利用EF核心内存提供程序。修改了original documentation示例,以在内置的依赖项注入中包括.Net核心。

  

然而,已经布置了上下文并再次实例化,但无法检索该对象,   背部。你能指出我正确的方向吗?

[TestMethod]
public void Find_searches_url()
{
    var options = new DbContextOptionsBuilder<BloggingContext>()
        .UseInMemoryDatabase(databaseName: "LkBlogs")
        .Options;
    ServiceCollection services = new ServiceCollection();
    services.AddDbContext<BloggingContext>((y) => y.UseInMemoryDatabase(databaseName: "LkBlogs"), ServiceLifetime.Transient);

    ServiceProvider serviceProvider = services.BuildServiceProvider();

    using (var context = serviceProvider.GetService<BloggingContext>())
    {
        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();
        var result = context.Blogs.Find(1);
        Assert.IsNotNull(result, "From same context (which was used for writing, could not retrieve)");
    }

    using (var context = serviceProvider.GetService<BloggingContext>())
    {
        var result = context.Blogs.Find(1);
        Assert.IsNotNull(result, "From new context, instantiated with DI could not retrieve");
    }

    using (var context = new BloggingContext(options))
    {
        var result = context.Blogs.FirstOrDefault();
        //Error: Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: 'Assert.IsNotNull failed. '
        Assert.IsNotNull(result, "From new context, instantiated without DI could not retrieve");
    }
}

编辑:文档示例:

[TestMethod]
        public void Find_searches_url()
        {
            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();
            }

            // Use a clean instance of the context to run the test
            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                var result = service.Find("cat");
                Assert.AreEqual(2, result.Count());
            }
        }

编辑:2 尝试了所有使用寿命,SingleTon,瞬态和作用域。例外保持不变。

编辑:3 按照@Chris的建议,删除了Context的usings。没有帮助:

[TestMethod]
    public void Find_searches_url()
    {
        var options = new DbContextOptionsBuilder<BloggingContext>()
            .UseInMemoryDatabase(databaseName: "LkBlogs")
            .Options;
        ServiceCollection services = new ServiceCollection();
        services.AddDbContext<BloggingContext>((y) => y.UseInMemoryDatabase(databaseName: "LkBlogs"),
            ServiceLifetime.Transient);

    ServiceProvider serviceProvider = services.BuildServiceProvider();

    var context = serviceProvider.GetService<BloggingContext>();
    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();
    var result = context.Blogs.Find(1);
    Assert.IsNotNull(result, "From same context (which was used for writing, could not retrieve)");

    var context2 = serviceProvider.GetService<BloggingContext>();
    var result2 = context2.Blogs.Find(1);
    Assert.IsNotNull(result2, "From new context, instantiated with DI could not retrieve");

    var context3 = new BloggingContext(options);
    var result3 = context3.Blogs.FirstOrDefault();
    //Error: Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: 'Assert.IsNotNull failed. '
    Assert.IsNotNull(result3, "From new context, instantiated without DI could not retrieve");
}

0 个答案:

没有答案