Tak zrobiłem to, zrobiłem wraper na Jsona. Zanim zaczniesz zacieszać, może zerkniesz czemu.
Albo może sam zobaczysz, jak on wygląda i domyślisz się czemu i co takiego mi to ułatwi:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsonWrapperSerializer : ISerializer | |
{ | |
public T Deserialize<T>(string myQueueItem) => Newtonsoft.Json.JsonConvert.DeserializeObject<T>(myQueueItem); | |
public string Serialize<T>(T element) => Newtonsoft.Json.JsonConvert.SerializeObject(element); | |
} |
I jak, masz pomysł?
Testowalność
Testy. Testowanie, a właściwie konfigurowanie takiej zależności jest banalnie prosta, nie trzeba mieć inline json czy plików z zserializowanymi danymi. Wystarczy wpis w kodzie (oraz dobra fixtura):
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var worker = fixture.MakeSerializerReturnChannelUpdate(description, title) | |
.GetWorker(); | |
var worker = fixtire.MakeSerializerReturnString(serialized) | |
.GetWorker(); |
Profit
Dzięki takiemu zabiegowi mam łatwiejsze podejście do danych, które muszę przygotować, aby testować kod, właściwie nie muszę ich przygotowywać, bo fixtura robi to za mnie i mam YOLO na to.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Theory, AutoData] | |
public void SerializerReturnUpdateObjectWhichIsUsedToUpdateChannelInformation(Function4WorkerFixture fixture, string item, string description, string title) | |
{ | |
var worker = fixture | |
.MakeSerializerReturnChannelUpdate(description, title) | |
.GetWorker(); | |
worker.RunAsync(item); | |
fixture.MockUpdater.Verify( | |
v => v.Update(It.Is<ChannelUpdate>(p => p.Description == description && p.Title == title)), Times.Once); | |
} |
Nie bójcie się wrapować systemowych zależności, idealnymi pozostałymi kandydatami to DateTime czy Guid