forked from ddrilling/AsbCloudServer
43 lines
1.7 KiB
C#
43 lines
1.7 KiB
C#
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)
|
|
where TDto : AsbCloudApp.Data.IId
|
|
where TRepository : class, ICrudRepository<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;
|
|
}
|
|
}
|
|
}
|