I am trying to do an integration test to test if my ApplicationDbContext is working properly. It is on a MVC core asp.net 3.1 application using entity framework and Pomelo.EntityFramework.MySql.
For the moment, I have just created the MVC application with ApplicationDbContext; An entity ; and I configured the database in startup.cs. At that time, I tested to migrate and update the database and it works.
After that, I added 2 projects in my solution for the unit test and the integration test, then adding the project reference on them. I did a simple test to use ApplicationDbContext but it doesn't work and I'm having trouble solving this problem.
If anyone has an idea on how to create this kind of test using xunit.
My project code is here: https://github.com/BeRoots/aspnetcore3.1-MVC-EF-MySQL
The integration test that I tryed to do is HERE
First, you will need to add the Pomelo.EntityFrameworkCore.MySql
NuGet package to your weblab2
and XUnitIntegrationTestWeblab2
projects.
Then you need to reference your weblab2
project from your XUnitIntegrationTestWeblab2
project.
Your project will now compile, but your test will still fail.
Finally, you will need to setup your ApplicationDbContext
instance, before using it.
I did this in form of a very simple example for the Application_DbContext_Should_Connect_To_MySql_Database
test:
[Fact]
public void Application_DbContext_Should_Connect_To_MySql_Database()
{
// Arrange
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseMySql("server=127.0.0.1;port=3306;user=root;password=;database=so59888273", builder => builder
.ServerVersion(new Version(8, 0, 18), ServerType.MySql));
ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options);
bool expected = true;
// Act
bool result = dbContext.Database.CanConnect();
// Assert
Assert.Equal(expected, result);
}
Here is a patch containing all the changes:
diff --git a/XUnitIntegrationTestWeblab2/UnitTest2.Pomelo.EntityFramework.Core.MySql.ApplicationDbContextTest.cs b/XUnitIntegrationTestWeblab2/UnitTest2.Pomelo.EntityFramework.Core.MySql.ApplicationDbContextTest.cs
index 2c60ea3..0b058a5 100644
--- a/XUnitIntegrationTestWeblab2/UnitTest2.Pomelo.EntityFramework.Core.MySql.ApplicationDbContextTest.cs
+++ b/XUnitIntegrationTestWeblab2/UnitTest2.Pomelo.EntityFramework.Core.MySql.ApplicationDbContextTest.cs
@@ -1,4 +1,6 @@
using System;
+using Microsoft.EntityFrameworkCore;
+using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using weblab2.Data;
using Xunit;
@@ -6,18 +8,24 @@ namespace XUnitIntegrationTestWeblab2
{
public class UnitTest2_Pomelo_EntityFramework_Core_MySql_ApplicationDbContextTest
{
- [Fact]
- public void Application_DbContext_Should_Connect_To_MySql_Database()
- {
- // Arrange
- ApplicationDbContext DbContext = new ApplicationDbContext();
- bool expected = true;
- // Act
- bool result = DbContext.Database.CanConnect();
+[Fact]
+public void Application_DbContext_Should_Connect_To_MySql_Database()
+{
+ // Arrange
+ var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>()
+ .UseMySql("server=127.0.0.1;port=3306;user=root;password=;database=so59888273", builder => builder
+ .ServerVersion(new Version(8, 0, 18), ServerType.MySql));
- // Assert
- Assert.Equal(expected, result);
- }
+ ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options);
+ bool expected = true;
+
+ // Act
+ bool result = dbContext.Database.CanConnect();
+
+ // Assert
+ Assert.Equal(expected, result);
+}
+
[Fact]
public async void Application_DbContext_Should_Connect_To_MySql_Database_Async()
{
diff --git a/XUnitIntegrationTestWeblab2/XUnitIntegrationTestWeblab2.csproj b/XUnitIntegrationTestWeblab2/XUnitIntegrationTestWeblab2.csproj
index a2352e4..665815c 100644
--- a/XUnitIntegrationTestWeblab2/XUnitIntegrationTestWeblab2.csproj
+++ b/XUnitIntegrationTestWeblab2/XUnitIntegrationTestWeblab2.csproj
@@ -8,9 +8,14 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
+ <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\weblab2\weblab2.csproj" />
+ </ItemGroup>
+
</Project>
diff --git a/weblab2/weblab2.csproj b/weblab2/weblab2.csproj
index 0fcc948..6a736ad 100644
--- a/weblab2/weblab2.csproj
+++ b/weblab2/weblab2.csproj
@@ -4,6 +4,10 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
+ <ItemGroup>
+ <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
+ </ItemGroup>
+
</Project>