Snippets

Joshua Go Reusable Mocks Version 2

Created by Joshua Go
// ***********************************************************************************************************
/*
VERSION 2

Make a InputService mock via inheritance.
Pass in the expected values, and override the AddInputRow()
*/
public class InputServiceMock : InputService
{
    public InputServiceMock(string client, int fileId)
        ExpectedClient = client;
        ExpectedFileId = fileId;
        Data = new StringBuilder();
    

    public string ExpectedClient { get; private set; }
    public int ExpectedFileId { get; private set; }
    public StringBuilder Data { get; private set; }

    public override void AddInputRow(string clientName, int inputFileId, Dictionary<string, string> values)
    {
        Assert.Equals(ExpectedClient, clientName);
        Assert.Equals(ExpectedFileId, inputFileId);

        if (Data.Length == 0)
            Data.AppendLine(string.Join(",", values.Keys));
        Data.AppendLine(string.Join(",", values.Values));
    }
}

[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
    var inputMock = new InputServiceMock(client, fileId);
    var converter = new FileToInputConverter(inputMock);
    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.