🚧 Work on UTs

pull/52/head
Alexis Drai 2 years ago
parent d7849eb366
commit c2457e339d

1
.gitignore vendored

@ -427,3 +427,4 @@ FodyWeavers.xsd
# Additional files built by Visual Studio
# End of https://www.toptal.com/developers/gitignore/api/dotnetcore,visualstudio,visualstudiocode
/Tests/Tests - Backup.csproj

@ -1,6 +1,16 @@
using cat_cafe.Controllers;
using AutoMapper;
using Castle.Core.Logging;
using cat_cafe.Controllers;
using cat_cafe.Dto;
using cat_cafe.Entities;
using cat_cafe.Mappers;
using cat_cafe.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NuGet.Protocol.Core.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,13 +19,60 @@ using System.Threading.Tasks;
namespace cat_cafe.Controllers.Tests
{
[TestClass()]
public class CatsControllerTest
{
private readonly ILogger<CatsController> logger = new NullLogger<CatsController>();
private readonly MapperConfiguration mapperConf = new(mapper => mapper.AddProfile(typeof(CatMapper)));
private readonly IMapper mapper;
private readonly DbContextOptions<CatContext> options = new DbContextOptionsBuilder<CatContext>()
.UseInMemoryDatabase(databaseName: "CatCafeTests")
.Options;
private readonly CatContext context;
private readonly CatsController controller;
public CatsControllerTest()
{
mapper = mapperConf.CreateMapper();
context = new CatContext(options);
controller = new CatsController(context, mapper, logger);
}
[TestInitialize]
public void StartUp()
{
context.Database.EnsureCreated();
context.Cats.AddRange(
new Cat
{
Id = 1,
Name = "Alice",
Age = 5
},
new Cat
{
Id = 2,
Name = "Bob",
Age = 3
});
context.SaveChanges();
// TODO tear down and drop all before each test method
}
[TestMethod()]
public void GetCatsTest()
public async Task GetCatsTest()
{
Assert.Fail();
ActionResult<IEnumerable<CatDto>> actual = await controller.GetCats();
Assert.Equals(200, actual.Result);
}
[TestMethod()]
@ -47,5 +104,6 @@ namespace cat_cafe.Controllers.Tests
{
Assert.Fail();
}
}
}

@ -10,9 +10,14 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\cat_cafe\cat_cafe.csproj" />
</ItemGroup>
</Project>

@ -9,6 +9,8 @@ using cat_cafe.Entities;
using cat_cafe.Repositories;
using AutoMapper;
using cat_cafe.Dto;
using Serilog;
using Newtonsoft.Json;
namespace cat_cafe.Controllers
{
@ -18,20 +20,27 @@ namespace cat_cafe.Controllers
{
private readonly CatContext _context;
private readonly IMapper _mapper;
private readonly ILogger<CatsController> _logger;
public CatsController(CatContext context, IMapper mapper)
public CatsController(CatContext context, IMapper mapper, ILogger<CatsController> logger)
{
_mapper = mapper;
_context = context;
_logger = logger;
}
// GET: api/Cats
[HttpGet]
public async Task<ActionResult<IEnumerable<CatDto>>> GetCats()
{
Log.Information(this.Request.Method + " => get All customers");
var cats = await _context.Cats.ToListAsync();
return Ok(_mapper.Map<List<CatDto>>(cats));
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ cats.GetType().ToString() + " length["
+ cats.Count + "]");
return Ok(_mapper.Map<List<CatDto>>(cats));
}
@ -39,15 +48,20 @@ namespace cat_cafe.Controllers
[HttpGet("{id}")]
public async Task<ActionResult<CatDto>> GetCat(long id)
{
Log.Information(this.Request.Method + " => get by ID {@id}", id);
var cat = await _context.Cats.FindAsync(id);
if (cat == null)
{
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
return NotFound();
}
Log.Information(this.Request.Method + " => "
+ this.Response.StatusCode.ToString() + " "
+ cat.GetType().ToString() + " "
+ JsonConvert.SerializeObject(cat).ToString());
return Ok(_mapper.Map<CatDto>(cat));
}
} //TODO finish logging
// PUT: api/Cats/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754

@ -1,9 +1,12 @@
namespace cat_cafe.Entities
using System.ComponentModel.DataAnnotations;
namespace cat_cafe.Entities
{
public class Cat
{
public long Id { get; set; }
public string? Name { get; set; }
[Required]
public int Age { get; set; } = 0;
public string Meow() { return "meow"; }

Loading…
Cancel
Save