public async Task UploadAsync_ShouldReturnFileNameWhenFileLenghtIsLessThanMax() { // ARRANGE var filesConfiguration = new FilesConfiguration { Directory = @"C:\Users\E7450\source\repos\TrainingDivision\TrainingDivisionKedis.BLL.Tests\bin\Debug\netcoreapp2.2\", MaxSize = 10000 }; IOptions <FilesConfiguration> options = Options.Create(filesConfiguration); _sut = new LocalFileService <FilesConfiguration>(options); var file = new Mock <IFormFile>(); var sourceImg = File.OpenRead(@"C:\Users\E7450\Pictures\handMade\64d735876ce855d858a742001e0585ea.jpg"); var ms = new MemoryStream(); var writer = new StreamWriter(ms); writer.Write(sourceImg); writer.Flush(); ms.Position = 0; var fileName = "QQ.png"; file.Setup(f => f.FileName).Returns(fileName).Verifiable(); file.Setup(f => f.Length).Returns(9999000).Verifiable(); file.Setup(_ => _.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .Returns((Stream stream, CancellationToken token) => ms.CopyToAsync(stream)) .Verifiable(); // ACT var actual = await _sut.UploadAsync(file.Object); // ASSERT Assert.Contains("QQ", actual); Assert.Contains(".png", actual); }
public async Task UploadAsync_ShouldThrowExceptionWhenFileLenghtIsBiggerThanMax() { // ARRANGE var filesConfiguration = new FilesConfiguration { Directory = @"C:\Users\E7450\source\repos\TrainingDivision\TrainingDivisionKedis.BLL.Tests\bin\Debug\netcoreapp2.2\", MaxSize = 10000 }; IOptions <FilesConfiguration> options = Options.Create(filesConfiguration); _sut = new LocalFileService <FilesConfiguration>(options); var file = new Mock <IFormFile>(); file.Setup(f => f.Length).Returns(10001000).Verifiable(); try { // ACT var actual = await _sut.UploadAsync(file.Object); } catch (Exception ex) { // ASSERT Assert.Equal("Размер файла должен быть не более " + filesConfiguration.MaxSize + "КБ.", ex.Message); } }