- `TestBuilder`-Klasse erstellt, um die Einrichtung der Abhängigkeitsinjektion für Unit-Tests zu vereinfachen. - `TestBuilder` so konfiguriert, dass Dienstregistrierungen und Datenbankkontext enthalten sind. - `TestBuilder` in das Test-Framework integriert, um eine einfache Bereitstellung von Diensten und Controllern zu ermöglichen.
90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
using DAL;
|
|
using DAL._Shared.SharedRepositories;
|
|
using HRD.WebApi;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Reflection;
|
|
using Xunit;
|
|
using XUnitWebApi.SharedConfig;
|
|
using XUnitWebApi.SharedUtils;
|
|
using XUnitWebApi.Test;
|
|
|
|
namespace XUnitWebApi.SahredDAL
|
|
{
|
|
public class Shared_Test_DAL : TestBuilder
|
|
{
|
|
[Fact]
|
|
public async System.Threading.Tasks.Task Check_Repository_XXXAsync()
|
|
{
|
|
Shared_Test_Config.Init_Webapi_Context();
|
|
WebAppUserRepository webAppUserRepository = Provider.GetRequiredService<WebAppUserRepository>();
|
|
|
|
var res = await webAppUserRepository.TakeListAsync(2);
|
|
Assert.NotEmpty(res);
|
|
}
|
|
|
|
[Fact]
|
|
public void Check_Repository_By_Name()
|
|
{
|
|
var res = Check_Repositories(0, true, "Case");
|
|
Assert.True(res);
|
|
}
|
|
|
|
[Fact]
|
|
public void Check_All_Repositories()
|
|
{
|
|
var res = Check_Repositories(0, true);
|
|
Assert.True(res);
|
|
}
|
|
|
|
[Fact]
|
|
public void Check_All_Repositories_With_Values()
|
|
{
|
|
var res = Check_Repositories(1, true);
|
|
Assert.True(res);
|
|
}
|
|
|
|
private bool Check_Repositories(int count = 0, bool raiseRepositoryExceptions = false, string repositoryName = "", string namespaceName = "DAL.Repositories")
|
|
{
|
|
Shared_Test_Config.Init_Webapi_Context();
|
|
|
|
WebApiConfig.RaiseRepositoryExceptions = raiseRepositoryExceptions;
|
|
|
|
string entityName = string.Empty;
|
|
|
|
List<string> ExceptionList = new List<string>();
|
|
|
|
WebApiContext ctx = Provider.GetRequiredService<WebApiContext>();
|
|
|
|
Assert.True(ctx.Database.CanConnect());
|
|
var typeList = TestHelper.GetAllRepositories(namespaceName);
|
|
|
|
foreach (var item in typeList)
|
|
{
|
|
try
|
|
{
|
|
if (repositoryName == "" || item.Name.Contains(repositoryName))
|
|
{
|
|
Type type = item as Type;
|
|
entityName = type.Name;
|
|
object instance = Activator.CreateInstance(type);
|
|
MethodInfo takeListMethod = type.GetMethod("TakeList");
|
|
dynamic list = takeListMethod.Invoke(instance, new object[] { count, true });
|
|
System.Diagnostics.Debug.WriteLine(entityName);
|
|
Assert.NotNull(list);
|
|
Assert.Equal(count, list.Count);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
ExceptionList.Add($"Entity: {entityName} " + ex.Message + " " + ex.InnerException);
|
|
}
|
|
}
|
|
|
|
var errList = "";
|
|
foreach (var item in ExceptionList) errList += "\r\n -- " + item;
|
|
if (ExceptionList.Count > 0) throw new Exception(errList);
|
|
return true;
|
|
}
|
|
}
|
|
} |