首页 > 解决方案 > 如何找出哪个nuget包导致异常?

问题描述

我正在尝试通过使用WebApplicationFactory类和 NUnit 块在我的 .net 5.0 项目中实现单元测试。

public class CustomWebApplicationFactory<TStartup>
    : WebApplicationFactory<TStartup> where TStartup: class
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            var descriptor = services.SingleOrDefault(
                d => d.ServiceType ==
                     typeof(DbContextOptions<AppDatabaseContext>));

            services.Remove(descriptor);

            services.AddDbContext<AppDatabaseContext>(options =>
            {
                options.UseInMemoryDatabase("InMemoryDbForTesting");
            });

            var sp = services.BuildServiceProvider();

            using (var scope = sp.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;
                var db = scopedServices.GetRequiredService<AppDatabaseContext>();
                var logger = scopedServices
                    .GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();

                db.Database.EnsureCreated();

                try
                {
                    Utilities.InitializeDbForTests(db);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "An error occurred seeding the " +
                                        "database with test messages. Error: {Message}", ex.Message);
                }
            }
        });
    }
}

我正在CustomWebApplicationFactory我的 Setup 方法中创建一个实例:

public class BaseTests
{
    protected HttpClient _client;
    protected CustomWebApplicationFactory<Startup> _customWebApplicationFactory;

    [OneTimeSetUp]
    public void Setup()
    {
        _customWebApplicationFactory = new CustomWebApplicationFactory<Startup>();
        _client = _customWebApplicationFactory.CreateClient();
    }
}

但是当我调用我的测试时,_client.GetAsync("myURL")我得到了这个错误:

System.MissingMethodException:找不到方法:
'System.Linq.Expressions.Expression DbSetAccessRewritingExpressionVisitor.Rewrite(Microsoft.EntityFrameworkCore.Metadata.IModel,System.Linq.Expressions.Expression)'。

我已经阅读了有关此异常的信息,并且从我发现它发生在 nuget 包不兼容/太旧时。我尝试更新所有已安装的 nuget 包,但没有帮助。如果这确实是由不兼容的 nuget 包引起的错误,我如何找出哪两个相互冲突?

标签: c#nunit

解决方案


我通过卸载作为预发布版本的 NuGet 包解决了这个问题。


推荐阅读