first commit

This commit is contained in:
2026-06-29 07:36:04 +02:00
commit 5e5b875e37
108 changed files with 13347 additions and 0 deletions
@@ -0,0 +1,113 @@
using AutoMapper;
using Dotnet.Samples.AspNetCore.WebApi.Controllers;
using Dotnet.Samples.AspNetCore.WebApi.Models;
using Dotnet.Samples.AspNetCore.WebApi.Repositories;
using Dotnet.Samples.AspNetCore.WebApi.Services;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Moq;
namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Utilities
{
/// <summary>
/// A Mock is a dynamic test double that not only provides responses but
/// also records interactions. Mocks are pre‑programmed with expectations
/// (like “this method should be called once with these parameters”) and can
/// later verify that the expected calls occurred. Frameworks such as Moq
/// make it easy to set up and verify these interactions.
/// </summary>
public static class PlayerMocks
{
/// <summary>
/// Initializes mocks for PlayerController dependencies.
/// </summary>
/// <returns>A tuple containing mocked service, logger, and validator.</returns>
public static (
Mock<IPlayerService> service,
Mock<ILogger<PlayerController>> logger,
Mock<IValidator<PlayerRequestModel>> validator
) InitControllerMocks()
{
var service = new Mock<IPlayerService>();
var logger = new Mock<ILogger<PlayerController>>();
var validator = new Mock<IValidator<PlayerRequestModel>>();
return (service, logger, validator);
}
/// <summary>
/// Creates a mock IUrlHelper configured to return any string for Action calls.
/// </summary>
/// <returns>A configured Mock of IUrlHelper.</returns>
public static Mock<IUrlHelper> SetupUrlHelperMock()
{
var mock = new Mock<IUrlHelper>();
mock.Setup(url => url.Action(It.IsAny<UrlActionContext>())).Returns(It.IsAny<string>());
return mock;
}
/// <summary>
/// Initializes mocks for PlayerService dependencies.
/// </summary>
/// <param name="cacheValue">Optional cache value to return from memory cache.</param>
/// <param name="environmentName">The environment name (defaults to "Development").</param>
/// <returns>A tuple containing mocked repository, logger, cache, mapper, and environment.</returns>
public static (
Mock<IPlayerRepository> repository,
Mock<ILogger<PlayerService>> logger,
Mock<IMemoryCache> memoryCache,
Mock<IMapper> mapper,
Mock<IHostEnvironment> environment
) InitServiceMocks(object? cacheValue = null, string environmentName = "Development")
{
var repository = new Mock<IPlayerRepository>();
var logger = new Mock<ILogger<PlayerService>>();
var memoryCache = SetupMemoryCacheMock(cacheValue ?? It.IsAny<object>());
var mapper = new Mock<IMapper>();
var environment = new Mock<IHostEnvironment>();
environment.Setup(env => env.EnvironmentName).Returns(environmentName);
return (repository, logger, memoryCache, mapper, environment);
}
/// <summary>
/// Creates a mock IMemoryCache with configurable behavior.
/// First TryGetValue call returns false, subsequent calls return true with the specified value.
/// </summary>
/// <param name="value">The value to return from cache after the first call.</param>
/// <returns>A configured Mock of IMemoryCache.</returns>
public static Mock<IMemoryCache> SetupMemoryCacheMock(object? value)
{
var cachedValue = false;
var mock = new Mock<IMemoryCache>();
mock.Setup(cache => cache.TryGetValue(It.IsAny<object>(), out It.Ref<object>.IsAny!))
.Callback(
new TryGetValueDelegate(
(object _, out object? output) =>
{
output = cachedValue ? value : null;
}
)
)
.Returns(() =>
{
bool hasValue = cachedValue;
cachedValue = true; // Subsequent invocations will return true
return hasValue;
});
mock.Setup(cache => cache.CreateEntry(It.IsAny<object>()))
.Returns(Mock.Of<ICacheEntry>);
mock.Setup(cache => cache.Remove(It.IsAny<object>()));
return mock;
}
private delegate void TryGetValueDelegate(object key, out object? value);
}
}