Skip to content

Instantly share code, notes, and snippets.

@GreenIcicle
Last active December 16, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreenIcicle/5433639 to your computer and use it in GitHub Desktop.
Save GreenIcicle/5433639 to your computer and use it in GitHub Desktop.
Code Example: Implementation of Builder pattern for test setups, used in http://blog.zuehlke.com/?p=1421
internal class ControllerBuilder
{
private IDataSource _dataSource;
private IEnumerable<string> _names = new[] {"Alice", "Bob", "Alfred"};
public ControllerBuilder WithDataSource(IDataSource dataSource)
{
_dataSource = dataSource;
return this;
}
public ControllerBuilder WithNames(params string[] names)
{
_names = names;
return this;
}
public Controller Build()
{
if (_dataSource == null)
{
var stub = new Mock<IDataSource>();
stub.Setup(x => x.GetNames()).Returns(_names);
_dataSource = stub.Object;
}
return new Controller(_dataSource);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment