Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 89 additions & 2 deletions ThingCache/ThingCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using FakeItEasy;
using NUnit.Framework;

namespace MockFramework
Expand Down Expand Up @@ -44,7 +45,8 @@ public class ThingCache_Should
[SetUp]
public void SetUp()
{
//thingService = A...
thingService = A.Fake<IThingService>();

thingCache = new ThingCache(thingService);
}

Expand All @@ -53,8 +55,92 @@ public void SetUp()

// Пример теста
[Test]
public void GiveMeAGoodNamePlease()
public void GetThingAndReturn()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Все тесты на месте, но я бы поработал над именованием тестов.

В названии хочется иметь три компонента: какой метод тестируем, какая ситуация и какое в ней ожидаемое поведение.

Например: Get_WhenThingExists_ReturnsIt, Get_WhenThingDoesNotExist_DoesNotCache

{
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();
}

/** Проверки в тестах
Expand All @@ -71,6 +157,7 @@ public void GiveMeAGoodNamePlease()
* A.CallTo(() => fake.TryRead(id, out value)).MustHaveHappened();
*/


/** Синтаксис out
* var value = "42";
* string _;
Expand Down
1 change: 1 addition & 0 deletions ThingCache/ThingCache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
</ItemGroup>

<ItemGroup>
Expand Down