Snippets

Joshua Go Reusable Mocks Version 1

Created by Joshua Go last modified
// ***********************************************************************************************************
/*
VERSION 1

Take Mock<InputService> and put it inside a function to create.
Pass in the expected values, and pass in a stringbuilder as a
reference, so it can be tested later
*/
public Mock<InputService> GetInputMock(string clientName, int inputFileId, StringBuilder stringBuilder)
{
    return new Mock<InputService>(MockBehavior.Strict);
    mockInputService.Setup(x => x.AddInputRow(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<Dictionary<string, string>>()))
        .Callback((string clientName, int inputFileId, Dictionary<string, string> values) =>
        {
            Assert.AreEqual(client, clientName);
            Assert.AreEqual(fileId, inputFileId);

            if (stringBuilder.Length == 0)
            {
                string headerRow = string.Join(", ", values.Keys);
                stringBuilder.AppendLine(headerRow);
            }

            string row = string.Join(", ", values.Values);
            stringBuilder.AppendLine(row);
        });
}

[Test, TestCaseSource("GetDSVCases")]
[UseReporter(typeof(DiffReporter))]
[UseApprovalSubdirectory("Approvals/FileToInputConverterTests_DSV")]
public void ParseDSVFiles(string client, string filePath, int fileId, InputDefinition inputDef)
{
    var mockInputFile = new Mock<InputFile>(MockBehavior.Strict);
    mockInputFile.Setup(x => x.GetData()).Returns(new FileStream(filePath, FileMode.Open));
    mockInputFile.Object.Id = fileId;

    // CHANGED HERE
    StringBuilder stringBuilder = new StringBuilder();
    var mockInputService = GetInputMock(client, inputFileId, stringBuilder);

    var converter = new FileToInputConverter(mockInputService.Object);
    converter.Parse(client, mockInputFile.Object, inputDef);

    using (ApprovalTests.Namers.ApprovalResults.ForScenario(inputDef.Name))
    {
        ApprovalTests.Approvals.Verify(stringBuilder.ToString());
    }
}
// ***********************************************************************************************************

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.