OneTimeSetUp: System.ArgumentNullException : Value cannot be null. (Parameter ‘connectionString’) -In Memory Database

I’m trying to set up an automated testing framework using NUnit and after I decided that going through the extreme pain of attempting to use Moq and specifically to set up some of the Moq behaviour handling (Async behaviour was the first big one and then Auto-Incrementing behaviour got me to quit trying to make it work and look for something else) was just not worth it long-term I eventually stumbled on using an In-Memory Database as one of the ways the data handling of the test scenario can be done, since from what I could understand it basically copies the structure and everything of the real DB with none of the data before then automatically dumping itself after the test scenario’s ran its course

Sounded pretty much exactly like how I wanted my test scenarios to behave in regards to data handling

So, as I was doing the setup for that thing by making sense of and stringing together what I could find out about the process online, I eventually ended up getting the error message in the title when attempting to run my test case

I’ve tried searching around for similar problems to mine but everything besides one SO thread was about something too different to really be of much help and that one thread which was also about an In-Memory DB with a similar issue as in the title turned out to have a problem entirely different from mine with their own setup of the thing

I’m stumped on how to deal with this, honestly

Here is what the onetime setup function roughly looks like in the test scenario class

private ServiceProvider _serviceProvider;

[OneTimeSetUp]
public void OneTimeSetUp()
{
    #region Service and DB setup
    var services = new ServiceCollection();

    //Instantiating the IConfiguration
    var configuration = new ConfigurationBuilder()
        .AddInMemoryCollection()
        .Build();

    //Mocking IWebHostEnvironment
    var envMock = new Mock<IWebHostEnvironment>() { CallBase = true };
    envMock.Setup(m => m.EnvironmentName).Returns("Development");
    envMock.Setup(m => m.ApplicationName).Returns("Web Platform");

    //Instantiating Startup with the mock web environment and the instantiated IConfiguration
    var startup = new Startup(configuration, envMock.Object);
    startup.ConfigureServices(services);

    //Overriding the DbContext registration to use an in-memory database for testing
    services.AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase("TestDb_Sc1"));

    _serviceProvider = services.BuildServiceProvider();
    #endregion

    //Seeding initial data
    using (var scope = _serviceProvider.CreateScope())
    {
        var scopedServices = scope.ServiceProvider;
        var dbContext = scopedServices.GetRequiredService<ApplicationDbContext>();

        var buyerTestData = new Buyer(){/*data*/};
        var topicTestData = new Topic(){/*data*/};


        dbContext.Buyers.Add(testBuyer);
        dbContext.Topics.Add(testTopic);
        dbContext.SaveChanges();
    }
}

Here is what the test case function in the test scenario looks like

[Test]
public async Task AdminAddsMessageToTopic()
{
    //Arrange
    using (var scope = _serviceProvider.CreateScope())
    {
        var scopedServices = scope.ServiceProvider;
        BuyerLogic manager = scopedServices.GetRequiredService<BuyerLogic>();
        var dbContext = scopedServices.GetRequiredService<ApplicationDbContext>();

        var messagePostModel = new MessagePostModel(){/*data*/};

        //Creating ClaimsPrincipal for the test case
        var claims = new[]
        {
            new Claim(ClaimTypes.NameIdentifier, "userIdString"),
            new Claim(ClaimTypes.Name, "userEmail")
        };
        ClaimsPrincipal user = new ClaimsPrincipal(new ClaimsIdentity(claims, "mock"));

        Uri referrer = new Uri("https://fakelink.com/fakepage");

        // Act
        Message addedMessage = await manager.AddMessage(messagePostModel, user, referrer);

        // Asserts
    }
}

Here is what the configuration function looks like in the Startup class

public void ConfigureServices(IServiceCollection services)
{
    services.AddHostedService<TestData>();
    services.AddDbContext<ApplicationDbContext>(options =>
    {
        options.UseSqlServer(Configuration.GetConnectionString("Database"));
        options.UseOpenIddict();
    });
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.Configure<IdentityOptions>(options =>
    {
        options.ClaimsIdentity.UserNameClaimType = Claims.Name;
        options.ClaimsIdentity.UserIdClaimType = Claims.Subject;
        options.ClaimsIdentity.RoleClaimType = Claims.Role;
        options.Password = new PasswordOptions()
        {
            //Options data
        };

    });

Here is what the connection strings looks like in my appsettings.json file

"ConnectionStrings": {
  "Database": "Data Source=.\SQLExpress;initial catalog=DB;Integrated Security=True"
},

Any ideas what the issue is? Or why the In-Memory DB’s even trying to get into the connection strings? Because my understading was that the In-Memory Database wasn’t supposed to interact with the real DB beyond the initial copying of its structure

New contributor

Boris Rusinov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật