using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.Cache; using Moq; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Xunit; namespace AsbCloudWebApi.Tests.ServicesTests { public class DrillerServiceTest { private readonly AsbCloudDbContext context; private readonly CacheDb cacheDb; private Driller driller = new Driller { Id = 1, Name = "Тестовый", Patronymic = "Тест", Surname = "Тестович" }; private DrillerDto drillerObj = new DrillerDto { Id = 0, Name = "Тестовый", Patronymic = "Тест", Surname = "Тестович" }; private DrillerService service; public DrillerServiceTest() { context = TestHelpter.MakeTestContext(); cacheDb = new CacheDb(); context.SaveChanges(); service = new DrillerService(context); } ~DrillerServiceTest() { } [Fact] public async Task GetListAsync_count() { ///Добавляем элемент var id = await service.InsertAsync(drillerObj, CancellationToken.None); id = await service.InsertAsync(drillerObj, CancellationToken.None); id = await service.InsertAsync(drillerObj, CancellationToken.None); var newCount = (await service.GetAllAsync(CancellationToken.None)).Count(); Assert.Equal(3, newCount); } [Fact] public async Task InsertAsync_returns_id() { var id = await service.InsertAsync(drillerObj, CancellationToken.None); Assert.NotEqual(0, id); } [Fact] public async Task UpdateAsync_not_add_if_exists() { ///Добавляем элемент context.Drillers.Add(driller); var oldCount = (await service.GetAllAsync(CancellationToken.None)).Count(); //Обновляем drillerObj.Id = driller.Id; drillerObj.Name = "Исправлено"; await service.UpdateAsync(drillerObj, CancellationToken.None); var newCount = (await service.GetAllAsync(CancellationToken.None)).Count(); Assert.Equal(newCount, oldCount); } } }