我正在构建一个简单的.NET Core Web Api应用程序,其中有一个后台任务,该任务从网站下载一些CSV文件,然后将它们保存到数据库中(使用EF Core)。另一个后台任务将处理它们。 这只是一个演示应用程序,让我熟悉.NET Core Web Api,EF Core,Moq,xUnit和存储库/服务/工作单元模式。
我已经写了业务逻辑并且可以正常工作,但是现在我也想进行测试。
这是我为测试排队功能而编写的测试:这非常简单。我检查文件是否已经排队(在特定日期),如果没有,我将其排队。
[Fact]
public void TestQueueFileForProcessingWhenNotYetQueued()
{
string alreadyQueuedFile = "Estrazione del 2019-01-02";
var mockUnitOfWork = new Mock<IUnitOfWork>();
var gasStationExternalFileRepository = new Mock<IGasStationExternalFileRepository>();
var service = new GasStationExternalFileService(mockUnitOfWork.Object, gasStationExternalFileRepository.Object);
gasStationExternalFileRepository.Setup(x => x.HasFileAlreadyBeenQueued(new DateTime(2019, 01, 02))).Returns(false);
var result = service.QueueFileForProcessing(Encoding.UTF8.GetBytes(alreadyQueuedFile));
Assert.True(result, "Object has been queued now");
var expectedFile = gasStationExternalFileRepository.Object.SingleOrDefault(x => x.ReferenceDate.Date == new DateTime(2019, 01, 02));
Assert.NotNull(expectedFile);
Assert.Equal(Encoding.UTF8.GetBytes(alreadyQueuedFile), expectedFile.Content);
Assert.Null(expectedFile.ProcessingDate);
Assert.Equal(Core.Reference.ProcessingStatus.Queued, expectedFile.ProcessingStatus);
Assert.Equal(new DateTime(2019, 01, 02), expectedFile.ReferenceDate);
}
这是基础的业务逻辑:
public class GasStationExternalFileService : IGasStationExternalFileService
{
public GasStationExternalFileService(IUnitOfWork unitOfWork, IGasStationExternalFileRepository gasStationExternalFileRepository)
{
this.unitOfWork = unitOfWork;
this.gasStationExternalFileRepository = gasStationExternalFileRepository;
}
readonly IUnitOfWork unitOfWork;
readonly IGasStationExternalFileRepository gasStationExternalFileRepository;
public bool QueueFileForProcessing(byte[] queue)
{
string fileContentAsText = System.Text.Encoding.UTF8.GetString(queue);
var firstRow = fileContentAsText.Split(new[] { '\r', '\n' }).First();
var referenceDate = DateTime.ParseExact(firstRow.Substring(15, 10), "yyyy-MM-dd", null);
var alreadyQueued = gasStationExternalFileRepository.HasFileAlreadyBeenQueued(referenceDate);
if (alreadyQueued)
{
return false;
}
else
{
var file = new GasStationsExternalFile();
file.Content = queue;
file.ReferenceDate = referenceDate;
file.ProcessingStatus = Core.Reference.ProcessingStatus.Queued;
gasStationExternalFileRepository.Add(file);
unitOfWork.Complete();
return true;
}
}
public void ProcessFile(Guid id)
{
throw new NotImplementedException();
}
}
我希望得到预期的文件
var expectedFile = gasStationExternalFileRepository.Object.SingleOrDefault(x => x.ReferenceDate.Date == new DateTime(2019, 01, 02));
不为null。但它始终为空。
我想念什么?
编辑:这是被测主题
public class GasStationExternalFileRepository : Repository<GasStationsExternalFile, DbContext>, IGasStationExternalFileRepository
{
public GasStationExternalFileRepository(DbContext context) : base(context)
{
}
public bool HasFileAlreadyBeenQueued(DateTime referenceDate)
{
return Entities.Where(x => x.ReferenceDate.Date == referenceDate).Select(x => x.Id).Any();
}
}
public abstract class Repository<TEntity, TContext> : IRepository<TEntity>
where TEntity : class, IEntity
where TContext : DbContext
{
public Repository(TContext context)
{
Entities = context.Set<TEntity>();
}
protected DbSet<TEntity> Entities { get; }
public TEntity Get(Guid id)
{
return Entities.Find(id);
}
public IEnumerable<TEntity> GetAll()
{
return Entities.ToList();
}
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return Entities.Where(predicate);
}
public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
{
return Entities.SingleOrDefault(predicate);
}
public void Add(TEntity entity)
{
Entities.Add(entity);
}
public void AddRange(IEnumerable<TEntity> entities)
{
Entities.AddRange(entities);
}
public void Remove(TEntity entity)
{
Entities.Remove(entity);
}
public void RemoveRange(IEnumerable<TEntity> entities)
{
Entities.RemoveRange(entities);
}
}