Skip to content

Instantly share code, notes, and snippets.

@pellared
Last active April 20, 2020 18:27
Show Gist options
  • Save pellared/a31b6955af5c61a56a277a50606c3410 to your computer and use it in GitHub Desktop.
Save pellared/a31b6955af5c61a56a277a50606c3410 to your computer and use it in GitHub Desktop.
Parameterized Object Mother in C# as an alternative to Test Data Builder pattern (thanks to named arguments with default values)
public static class AddressObjectMother
{
public static Address Create(string street = "", string city = "", PostCode postCode = null)
{
if (postCode == null)
{
postCode = PostCodeObjectMother.Create();
}
return new Address(street, city, postCode);
}
}
public static class PostCodeObjectMother
{
public static PostCode Create(string postCode = "NL-1000")
{
return new PostCode(postCode);
}
public static PostCode CreateEmpty()
{
return new PostCode();
}
}
var address = AddressObjectMother.Create(
postCode: PostCodeObjectMother.CreateEmpty()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment