Skip to content

Instantly share code, notes, and snippets.

@es-repo
es-repo / better-unit-test-in-c#-article-FillBoxTest.cs
Created July 28, 2022 22:11
better-unit-test-in-c#-article-FillBoxTest.cs
public static class FillBoxTest
{
public sealed record Args
{
public IBox Box { get; init; } = null!;
public Dictionary<string, Thing> LabelsAndThings { get; init; } = new();
public WriteLog WriteLog { get; init; } = null!;
}
sealed class TestCases : IEnumerable<object[]>
static object[] BoxAndLabelsAndThings_OpenBoxThenPutThingInsideThenCloseBoxAndWriteLogs_ThingsWithLabelEndedWithIgnoreExpected_1()
{
var writeLogMock = new Mock<WriteLog>(MockBehavior.Strict);
var boxMock = new Mock<IBox>(MockBehavior.Strict);
var mockSequence = new MockSequence();
boxMock.InSequence(mockSequence).Setup(box => box.Open());
writeLogMock.InSequence(mockSequence).Setup(writeLog => writeLog("The box is opened."));
boxMock.InSequence(mockSequence).Setup(box => box.PutInside(new Thing { Size = 1 }, "Label 1")).Returns(true);
@es-repo
es-repo / better-unit-test-in-c#-article-mock-sequences.cs
Last active July 28, 2022 22:23
better-unit-test-in-c#-article-mock-sequences.cs
var mockSequence = new MockSequence();
boxMock.InSequence(mockSequence).Setup(box => box.Open());
writeLogMock.InSequence(mockSequence).Setup(writeLog => writeLog("The box is opened."));
boxMock.InSequence(mockSequence).Setup(box => box.PutInside(new Thing { Size = 1 }, "Label 1")).Returns(true);
boxMock.InSequence(mockSequence).Setup(box => box.PutInside(new Thing { Size = 2 }, "Label 2 Ignore")).Returns(false);
boxMock.InSequence(mockSequence).Setup(box => box.PutInside(new Thing { Size = 3 }, "Label 3")).Returns(true);
boxMock.InSequence(mockSequence).Setup(box => box.Close());
writeLogMock.InSequence(mockSequence).Setup(writeLog => writeLog("The box is closed."));
@es-repo
es-repo / better-unit-test-in-c#-article-mock-sequence.cs
Last active July 28, 2022 22:04
better-unit-test-in-c#-article-mock-sequence.cs
var writeLogMock = new Mock<WriteLog>(MockBehavior.Strict);
var boxMock = new Mock<IBox>(MockBehavior.Strict);
boxMock.InSequence(mockSequence).Setup(box => box.Open());
writeLogMock.InSequence(mockSequence).Setup(writeLog => writeLog("The box is opened."));
boxMock.InSequence(mockSequence).Setup(box => box.PutInside(new Thing { Size = 1 }, "Label 1")).Returns(true);
boxMock.InSequence(mockSequence).Setup(box => box.PutInside(new Thing { Size = 2 }, "Label 2 Ignore")).Returns(false);
boxMock.InSequence(mockSequence).Setup(box => box.PutInside(new Thing { Size = 3 }, "Label 3")).Returns(true);
boxMock.InSequence(mockSequence).Setup(box => box.Close());
writeLogMock.InSequence(mockSequence).Setup(writeLog => writeLog("The box is closed."));
@es-repo
es-repo / better-unit-test-in-c#-article-mocks.cs
Created July 28, 2022 21:59
better-unit-test-in-c#-article-mocks.cs
var writeLogMock = new Mock<WriteLog>(MockBehavior.Strict);
var boxMock = new Mock<IBox>(MockBehavior.Strict);
@es-repo
es-repo / better-unit-test-in-c#-article-FillBoxTest-TestCases-layout.cs
Last active July 26, 2022 22:59
better-unit-test-in-c#-article-FillBoxTest-TestCases-layout.cs
sealed class TestCases : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return BoxAndLabelsAndThings_OpenBoxThenPutThingInsideThenCloseBoxAndWriteLogs_ThingsWithLabelEndedWithIgnoreExpected_1();
}
static object[] BoxAndLabelsAndThings_OpenBoxThenPutThingInsideThenCloseBoxAndWriteLogs_ThingsWithLabelEndedWithIgnoreExpected_1()
{
...
@es-repo
es-repo / better-unit-test-in-c#-article-FillBoxTest-layout.cs
Last active July 28, 2022 21:54
better-unit-test-in-c#-article-FillBoxTest-layout.cs
public static class FillBoxTest
{
public sealed record Args
{
public IBox Box { get; init; } = null!;
public Dictionary<string, Thing> LabelsAndThings { get; init; } = new();
public WriteLog WriteLog { get; init; } = null!;
}
sealed class TestCases : IEnumerable<object[]>
@es-repo
es-repo / better-unit-test-in-c#-article-Operations-FillBox.cs
Last active July 28, 2022 21:49
better-unit-test-in-c#-article-Operations-FillBox.cs
public static class Operations
{
public static Dictionary<string, Thing> FillBox(
IBox box,
IDictionary<string, Thing> labelsAndThings,
WriteLog writeLog)
{
var rest = new Dictionary<string, Thing>();
box.Open();
writeLog("The box is opened.");
@es-repo
es-repo / better-unit-test-in-c#-article-IBox-and-WriteLog.cs
Last active July 28, 2022 21:48
better-unit-test-in-c#-article-IBox-and-WriteLog.cs
public interface IBox
{
int Size { get; }
void Open();
void Close();
bool PutInside(Thing thing, string label);
}
public delegate void WriteLog(string message);
@es-repo
es-repo / better-unit-test-in-c#-article-PutInsideTest-full.cs
Last active May 22, 2022 22:02
better-unit-test-in-c#-article-PutInsideTest-full.cs
public static class PutInsideTest
{
public sealed record Args
{
public Thing Thing { get; init; } = null!;
public string Label { get; init; } = "";
}
sealed class TestCases : IEnumerable<object[]>
{