From 4d2c3c798f1eb53519d2f0ac064f25b1909d758a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=91=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2?= Date: Fri, 11 Dec 2020 14:48:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=91?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D1=82=D0=BE=D0=B2.=20=D0=97=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B0=20Thing=20Cache=20=D0=BD=D0=B0=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D1=82=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThingCache/ThingCache.cs | 91 +++++++++++++++++++++++++++++++++++- ThingCache/ThingCache.csproj | 1 + 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/ThingCache/ThingCache.cs b/ThingCache/ThingCache.cs index 2d3859d..ff2823c 100644 --- a/ThingCache/ThingCache.cs +++ b/ThingCache/ThingCache.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using FakeItEasy; using NUnit.Framework; namespace MockFramework @@ -44,7 +45,8 @@ public class ThingCache_Should [SetUp] public void SetUp() { - //thingService = A... + thingService = A.Fake(); + thingCache = new ThingCache(thingService); } @@ -53,8 +55,92 @@ public void SetUp() // Пример теста [Test] - public void GiveMeAGoodNamePlease() + public void GetThingAndReturn() + { + A.CallTo(() => thingService.TryRead(thingId1, out thing1)) + .Returns(true); + + var actualThing = thingCache.Get(thingId1); + + Assert.AreEqual(thingId1, actualThing.ThingId); + } + + [Test] + public void GetNotExistThing() + { + Thing thing; + A.CallTo(() => thingService.TryRead(thingId1, out thing)) + .Returns(false); + + var actualThing = thingCache.Get(thingId1); + + Assert.AreEqual(null, actualThing); + } + + [Test] + public void GetNotExistThingTwice() + { + Thing thing; + A.CallTo(() => thingService.TryRead(thingId1, out thing)) + .Returns(false); + + thingCache.Get(thingId1); + var secondThing1 = thingCache.Get(thingId1); + + Assert.AreEqual(null, secondThing1); + } + + [Test] + public void GetNotExistThingTwiceMore() + { + Thing thing; + A.CallTo(() => thingService.TryRead(thingId1, out thing)) + .Returns(false); + + thingCache.Get(thingId1); + thingCache.Get(thingId1); + + A.CallTo(() => thingService.TryRead(thingId1, out thing)) + .MustHaveHappenedTwiceExactly(); + } + + [Test] + public void GetExistThingTwice() + { + A.CallTo(() => thingService.TryRead(thingId1, out thing1)) + .Returns(true); + + var firstCallThing = thingCache.Get(thingId1); + var secondCallThing = thingCache.Get(thingId1); + + Assert.AreEqual(thingId1, firstCallThing.ThingId); + Assert.AreEqual(thingId1, secondCallThing.ThingId); + Thing thing; + A.CallTo(() => thingService.TryRead(thingId1, out thing)) + .MustHaveHappenedOnceExactly(); + } + + [Test] + public void GetExistSeveralThings() { + A.CallTo(() => thingService.TryRead(thingId1, out thing1)) + .Returns(true); + A.CallTo(() => thingService.TryRead(thingId2, out thing2)) + .Returns(true); + + thingCache.Get(thingId1); + thingCache.Get(thingId2); + var secondCallThing1 = thingCache.Get(thingId1); + var secondCallThing2 = thingCache.Get(thingId2); + + Assert.AreEqual(thingId1, secondCallThing1.ThingId); + Assert.AreEqual(thingId2, secondCallThing2.ThingId); + + Thing thing; + A.CallTo(() => thingService.TryRead(thingId1, out thing)) + .MustHaveHappenedOnceExactly(); + A.CallTo(() => thingService.TryRead(thingId2, out thing)) + .MustHaveHappenedOnceExactly(); } /** Проверки в тестах @@ -71,6 +157,7 @@ public void GiveMeAGoodNamePlease() * A.CallTo(() => fake.TryRead(id, out value)).MustHaveHappened(); */ + /** Синтаксис out * var value = "42"; * string _; 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 @@ +