From 58dc1bde354f3b6664979c72a14c666b9b0093c4 Mon Sep 17 00:00:00 2001 From: kosukhin Date: Tue, 13 Apr 2021 13:13:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=BA=D1=83=D1=80=D1=81=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FileSender/FileSender.cs | 87 +++++++++++++++++++++++++++--------- FileSender/FileSender.csproj | 1 + ThingCache/ThingCache.cs | 47 ++++++++++++++++++- ThingCache/ThingCache.csproj | 1 + 4 files changed, 113 insertions(+), 23 deletions(-) diff --git a/FileSender/FileSender.cs b/FileSender/FileSender.cs index 5899292..e239bdd 100644 --- a/FileSender/FileSender.cs +++ b/FileSender/FileSender.cs @@ -73,7 +73,10 @@ public class FileSender_Should private readonly X509Certificate certificate = new X509Certificate(); private File file; + private File file2; private byte[] signedContent; + private byte[] signedContent2; + //private Document defaultDocument; [SetUp] public void SetUp() @@ -82,12 +85,31 @@ public void SetUp() // чтобы в конкретных тестах осталась только специфика теста, // без конфигурирования "обычного" сценария работы - file = new File("someFile", new byte[] {1, 2, 3}); - signedContent = new byte[] {1, 7}; + file = new File("someFile", new byte[] { 1, 2, 3 }); + file2 = new File("someFile2", new byte[] { 1, 2, 3, 4 }); + //var document = new Document(file.Name, file.Content, DateTime.Now, "4.0"); + signedContent = new byte[] { 1, 7 }; + signedContent2 = new byte[] { 1, 7, 2 }; cryptographer = A.Fake(); + A.CallTo(() => cryptographer.Sign(file.Content, certificate)) + .Returns(signedContent); + A.CallTo(() => cryptographer.Sign(file2.Content, certificate)) + .Returns(signedContent2); + sender = A.Fake(); + A.CallTo(() => sender.TrySend(signedContent)) + .WithAnyArguments() + .Returns(true); + + var document = new Document(file.Name, file.Content, DateTime.Now, "4.0"); + var document2 = new Document(file2.Name, file2.Content, DateTime.Now, "4.0"); recognizer = A.Fake(); + A.CallTo(() => recognizer.TryRecognize(file, out document)) + .Returns(true); + A.CallTo(() => recognizer.TryRecognize(file2, out document2)) + .Returns(true); + fileSender = new FileSender(cryptographer, sender, recognizer); } @@ -98,62 +120,85 @@ public void Send_WhenGoodFormat(string format) var document = new Document(file.Name, file.Content, DateTime.Now, format); A.CallTo(() => recognizer.TryRecognize(file, out document)) .Returns(true); - A.CallTo(() => cryptographer.Sign(document.Content, certificate)) - .Returns(signedContent); - A.CallTo(() => sender.TrySend(signedContent)) - .Returns(true); - fileSender.SendFiles(new[] {file}, certificate) + fileSender.SendFiles(new[] { file }, certificate) .SkippedFiles.Should().BeEmpty(); } [Test] - [Ignore("Not implemented")] public void Skip_WhenBadFormat() { - throw new NotImplementedException(); + string format = "3.0"; + var document = new Document(file.Name, file.Content, DateTime.Now, format); + A.CallTo(() => recognizer.TryRecognize(file, out document)) + .Returns(true); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().NotBeEmpty(); } [Test] - [Ignore("Not implemented")] public void Skip_WhenOlderThanAMonth() { - throw new NotImplementedException(); + var document = new Document(file.Name, file.Content, DateTime.Now.AddMonths(-1).AddSeconds(-1), "4.0"); + A.CallTo(() => recognizer.TryRecognize(file, out document)) + .Returns(true); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().NotBeEmpty(); } [Test] - [Ignore("Not implemented")] public void Send_WhenYoungerThanAMonth() { - throw new NotImplementedException(); + var document = new Document(file.Name, file.Content, DateTime.Now.AddMonths(-1).AddSeconds(1), "4.0"); + A.CallTo(() => recognizer.TryRecognize(file, out document)) + .Returns(true); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().BeEmpty(); } [Test] - [Ignore("Not implemented")] public void Skip_WhenSendFails() { - throw new NotImplementedException(); + A.CallTo(() => sender.TrySend(signedContent)) + .Returns(false); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().NotBeEmpty(); } [Test] - [Ignore("Not implemented")] public void Skip_WhenNotRecognized() { - throw new NotImplementedException(); + Document _ = null; + A.CallTo(() => recognizer.TryRecognize(file, out _)) + .Returns(false); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().NotBeEmpty(); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeAreInvalid() { - throw new NotImplementedException(); + Document _ = null; + A.CallTo(() => recognizer.TryRecognize(file2, out _)) + .Returns(false); + + fileSender.SendFiles(new[] { file, file2 }, certificate) + .SkippedFiles.Should().BeEquivalentTo(file2); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeCouldNotSend() { - throw new NotImplementedException(); + A.CallTo(() => sender.TrySend(signedContent2)) + .Returns(false); + + fileSender.SendFiles(new[] { file, file2 }, certificate) + .SkippedFiles.Should().BeEquivalentTo(file2); } } } diff --git a/FileSender/FileSender.csproj b/FileSender/FileSender.csproj index b4c7dfe..b0c413e 100644 --- a/FileSender/FileSender.csproj +++ b/FileSender/FileSender.csproj @@ -11,6 +11,7 @@ + diff --git a/ThingCache/ThingCache.cs b/ThingCache/ThingCache.cs index 2d3859d..0d3d2e4 100644 --- a/ThingCache/ThingCache.cs +++ b/ThingCache/ThingCache.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using FakeItEasy; +using FluentAssertions; using NUnit.Framework; namespace MockFramework @@ -44,8 +46,17 @@ public class ThingCache_Should [SetUp] public void SetUp() { - //thingService = A... + thingService = A.Fake(); thingCache = new ThingCache(thingService); + + Thing _ = null; + A.CallTo(() => thingService.TryRead(thingId1, out _)) + .Returns(true) + .AssignsOutAndRefParameters(thing1); + + A.CallTo(() => thingService.TryRead(thingId2, out _)) + .Returns(true) + .AssignsOutAndRefParameters(thing2); } // TODO: Написать простейший тест, а затем все остальные @@ -53,8 +64,40 @@ public void SetUp() // Пример теста [Test] - public void GiveMeAGoodNamePlease() + public void CallThingServiceOnce_OnFirstGet() + { + thingCache.Get(thingId1); + + Thing _ = null; + + A.CallTo(() => thingService.TryRead(thingId1, out _)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void CallThingServiceOnce_OnTwoGet() + { + thingCache.Get(thingId1); + thingCache.Get(thingId1); + + Thing _ = null; + + A.CallTo(() => thingService.TryRead(thingId1, out _)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void ReturnRightThing_OnGet() + { + var thing = thingCache.Get(thingId1); + + thing.Should().Be(thing1); + } + + [Test] + public void ReturnNull_OnEmptyId() { + var thing = thingCache.Get(""); + + thing.Should().BeNull(); } /** Проверки в тестах diff --git a/ThingCache/ThingCache.csproj b/ThingCache/ThingCache.csproj index 6d6bed7..c915cd3 100644 --- a/ThingCache/ThingCache.csproj +++ b/ThingCache/ThingCache.csproj @@ -12,6 +12,7 @@ +