DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/RepositoryFactory.cs

43 lines
1.6 KiB
C#
Raw Normal View History

2022-10-05 15:47:02 +05:00
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<TRepository> Make<TRepository, TDto>(ICollection<TDto> data)
2022-10-05 15:47:02 +05:00
where TDto : AsbCloudApp.Data.IId
where TRepository : class, ICrudService<TDto>
{
var repositoryMock = new Mock<TRepository>();
repositoryMock.Setup(x => x.InsertAsync(It.IsAny<TDto>(), It.IsAny<CancellationToken>()))
.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<int>(), It.IsAny<CancellationToken>()))
.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<CancellationToken>())).ReturnsAsync(data);
repositoryMock.Setup(x => x.GetOrDefaultAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns((int idFile, CancellationToken token) => {
return Task.FromResult(data.FirstOrDefault(x => x.Id == idFile));
});
return repositoryMock;
}
}
}