Last active
December 16, 2015 12:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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