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
87 changes: 66 additions & 21 deletions FileSender/FileSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<ICryptographer>();
A.CallTo(() => cryptographer.Sign(file.Content, certificate))
.Returns(signedContent);
A.CallTo(() => cryptographer.Sign(file2.Content, certificate))
.Returns(signedContent2);

sender = A.Fake<ISender>();
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<IRecognizer>();
A.CallTo(() => recognizer.TryRecognize(file, out document))
.Returns(true);
A.CallTo(() => recognizer.TryRecognize(file2, out document2))
.Returns(true);

fileSender = new FileSender(cryptographer, sender, recognizer);
}

Expand All @@ -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)
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.

В таких тестах неплохо бы ещё проверять, что не было вызова TrySend: сайд-эффект здесь важнее, чем возвращаемое значение

.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);
}
}
}
1 change: 1 addition & 0 deletions FileSender/FileSender.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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
47 changes: 45 additions & 2 deletions ThingCache/ThingCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using FakeItEasy;
using FluentAssertions;
using NUnit.Framework;

namespace MockFramework
Expand Down Expand Up @@ -44,17 +46,58 @@ public class ThingCache_Should
[SetUp]
public void SetUp()
{
//thingService = A...
thingService = A.Fake<IThingService>();
thingCache = new ThingCache(thingService);

Thing _ = null;
A.CallTo(() => thingService.TryRead(thingId1, out _))
.Returns(true)
.AssignsOutAndRefParameters(thing1);
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.

Я бы делал это не в сетапе, а в самих тестах. Сейчас по коду самого теста непонятно, что это за thing1, есть оно в сервисе или нет. Или назвать как-нибудь типа existingThing


A.CallTo(() => thingService.TryRead(thingId2, out _))
.Returns(true)
.AssignsOutAndRefParameters(thing2);
}

// TODO: Написать простейший тест, а затем все остальные
// Live Template tt работает!

// Пример теста
[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()
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.

В коде нет отдельной обработки пустой строки: если по пустой строке что-то лежит, то оно вернётся.

Тест все равно зелёный, потому что в данном случае по пустой строке ничего нет. Можно переделать его на тест, проверяющий, обратное (что пустая строка работает). И добавить тест, проверяющий, что если по произвольному ключу ничего нет, то вернется null

{
var thing = thingCache.Get("");

thing.Should().BeNull();
}
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.

Тут бы ещё пару тестов написать: проверить кейс, когда несколько сущностей, и проверить, кэшируется ли null в случае отсутствия сущности в сервисе


/** Проверки в тестах
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