I'm trying to test a method that takes a dictionary as a parameter, filters it by removing some unnecessary entries and returns the resulting dictionary. I have separately written the tests for the filtering class working correctly so this test is only concerned with the filterer being called and returning the dictionary.
For some reason, the filterer returns empty dictionary even though I specifically tell it what to return with FakeItEasy.
The _callToChartOfAccountsFilterer
assertion fails with the following message:
Assertion failed for the following call: TinyBooks.Services.Json.ChartOfAccounts.IChartOfAccountsFilterer.FilterAccounts(dictionary: Ignored, filteringParameters: Ignored) Expected to find it once exactly but didn't find it among the calls: 1: TinyBooks.Services.Json.ChartOfAccounts.IChartOfAccountsFilterer.FilterAccounts(dictionary: [[Group1, System.Collections.Generic.List`1[TinyBooks.DomainModel.Dtos.AccountForParserDto]]], filteringParameters: TinyBooks.Services.Json.ChartOfAccounts.ChartOfAccountsFilteringParameters)
ChartOfAccountsParser
public class ChartOfAccountsParser : IChartOfAccountsParser
{
private readonly IJsonDeserializer _jsonDeserializer;
private readonly IChartOfAccountsFilterer _chartOfAccountsFilterer;
public ChartOfAccountsParser(IJsonDeserializer jsonDeserializer, IChartOfAccountsFilterer chartOfAccountsFilterer)
{
_jsonDeserializer = jsonDeserializer;
_chartOfAccountsFilterer = chartOfAccountsFilterer;
}
public Dictionary<string, List<AccountForParserDto>> GetChartOfAccountsDictionaryBasedOnJsonFile(
ChartOfAccountsParsingParameters parsingParameters)
{
var filePath = GetPathOfJsonFile(parsingParameters.FileName);
var dictionary = ParseJsonToDictionary(filePath);
var dic = _chartOfAccountsFilterer.FilterAccounts(dictionary, (ChartOfAccountsFilteringParameters)parsingParameters);
return dic;
}
private string GetPathOfJsonFile(string fileName) => Path.Combine(GetFolderOfExecutingAssembly(), "ChartOfAccounts\\Json", fileName);
private string GetFolderOfExecutingAssembly()
=> new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new InvalidOperationException()).LocalPath;
private Dictionary<string, List<AccountForParserDto>> ParseJsonToDictionary(string filePath)
=> _jsonDeserializer.Load<Dictionary<string, List<AccountForParserDto>>>(filePath);
}
ChartOfAccountsParserTests
[TestFixture]
public class ChartOfAccountsParserTests
{
private IJsonDeserializer _jsonDeserializer;
private IChartOfAccountsFilterer _chartOfAccountsFilterer;
private AccountForParserDto _dto;
private ChartOfAccountsParsingParameters _parameters;
private string _key;
private List<AccountForParserDto> _group;
private Dictionary<string, List<AccountForParserDto>> _dictionary;
private IReturnValueArgumentValidationConfiguration<Dictionary<string, List<AccountForParserDto>>> _callToJsonDeserializer;
private IReturnValueArgumentValidationConfiguration<Dictionary<string, List<AccountForParserDto>>> _callToChartOfAccountsFilterer;
[SetUp]
public void Setup()
{
_jsonDeserializer = A.Fake<IJsonDeserializer>();
_chartOfAccountsFilterer = A.Fake<IChartOfAccountsFilterer>();
_dto = new AccountForParserDto { Name = "Account1" };
_parameters = new ChartOfAccountsParsingParameters { FileName = "a.json" };
_key = "Group1";
_group = new List<AccountForParserDto> { _dto };
_dictionary = new Dictionary<string, List<AccountForParserDto>> { [_key] = _group} ;
_callToJsonDeserializer = A.CallTo(() => _jsonDeserializer.Load<Dictionary<string, List<AccountForParserDto>>>(A<string>._));
_callToChartOfAccountsFilterer = A.CallTo(() => _chartOfAccountsFilterer.FilterAccounts(
A<Dictionary<string, List<AccountForParserDto>>>._,
A<ChartOfAccountsParsingParameters>._));
_callToJsonDeserializer.Returns(_dictionary);
_callToChartOfAccountsFilterer.Returns(_dictionary);
}
[Test]
public void GetChartOfAccountsDictionaryBasedOnJsonFile_GivenDictionaryIgnoringParameters_ReturnsSameDictionary()
{
var parser = new ChartOfAccountsParser(_jsonDeserializer, _chartOfAccountsFilterer);
var result = parser.GetChartOfAccountsDictionaryBasedOnJsonFile(_parameters);
Assert.That(result.ContainsKey(_key));
Assert.That(result.ContainsValue(_group));
_callToJsonDeserializer.MustHaveHappenedOnceExactly();
_callToChartOfAccountsFilterer.MustHaveHappenedOnceExactly();
}
}