Reputation: 1
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
Upvotes: 0
Views: 216