Files

52 lines
1.9 KiB
C#

using GameAuthApi.Controllers;
using GameAuthApi.Models;
using GameAuthApi.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
public class ItemControllerTests
{
private static IConfiguration config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string,string>
{
{"MongoDB:ConnectionString","mongodb://localhost"},
{"MongoDB:DatabaseName","test"}
}).Build();
[Fact]
public async Task Create_InvalidLocation_ReturnsBadRequest()
{
var loc = new Mock<LocationService>(config);
loc.Setup(l => l.GetByIdAsync("bad")).ReturnsAsync((Location?)null);
var controller = new ItemController(new Mock<ItemService>(config).Object, loc.Object, new Mock<SpawnableService>(config).Object);
var result = await controller.Create(new CreateItemRequest { LocationId = "bad", Name = "n" });
Assert.IsType<BadRequestObjectResult>(result);
}
[Fact]
public async Task Update_ItemNotFound()
{
var svc = new Mock<ItemService>(config);
svc.Setup(i => i.GetByIdAsync("id")).ReturnsAsync((Item?)null);
var controller = new ItemController(svc.Object, new Mock<LocationService>(config).Object, new Mock<SpawnableService>(config).Object);
var result = await controller.Update("id", new UpdateItemRequest { LocationId="loc", Name="x" });
Assert.IsType<NotFoundObjectResult>(result);
}
[Fact]
public async Task Delete_NotFound()
{
var svc = new Mock<ItemService>(config);
svc.Setup(i => i.GetByIdAsync("id")).ReturnsAsync((Item?)null);
var controller = new ItemController(svc.Object, new Mock<LocationService>(config).Object, new Mock<SpawnableService>(config).Object);
var result = await controller.Delete("id");
Assert.IsType<NotFoundObjectResult>(result);
}
}