using AsbCloudApp.Services; using Moq; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Tests { public class RepositoryFactory { public static Mock Make(ICollection data) where TDto : AsbCloudApp.Data.IId where TRepository : class, ICrudService { var repositoryMock = new Mock(); repositoryMock.Setup(x => x.InsertAsync(It.IsAny(), It.IsAny())) .Returns((TDto dto, CancellationToken token) => { var id = data.Max(x => x.Id); dto.Id = ++id; data.Add(dto); return Task.FromResult(dto.Id); }); repositoryMock.Setup(x => x.DeleteAsync(It.IsAny(), It.IsAny())) .Returns((int idFile, CancellationToken token) => { var cnt = data.Count; var dto = data.FirstOrDefault(x => x.Id == idFile); data.Remove(dto); return Task.FromResult(cnt - data.Count); }); repositoryMock.Setup(x => x.GetAllAsync(It.IsAny())).ReturnsAsync(data); repositoryMock.Setup(x => x.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .Returns((int idFile, CancellationToken token) => { return Task.FromResult(data.FirstOrDefault(x => x.Id == idFile)); }); return repositoryMock; } } }