Created
October 29, 2012 12:10
-
-
Save zsoldosp/3973209 to your computer and use it in GitHub Desktop.
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
// re: for http://codemanship.co.uk/parlezuml/blog/?postid=1149 | |
// original code, called from inside the method: | |
Customer customer = DataRepository.GetCustomer(customerId); | |
// to enable the creation of the regression suite, | |
// I extract the implementation of DataRepository.GetCustomer into its own methpd | |
class DataRepository { | |
public static Customer GetCustomer(customerId) { | |
return DataRepository.getCustomer(customerId); | |
} | |
private static Customer getCustomer(customerId) { | |
return ... | |
} | |
} | |
// now I change the public static method into a Func<Customer, int> property and | |
// initialize it to the private method | |
class DataRepository { | |
public static Func<Customer, int> GetCustomer = DataRepository.getCustomer; | |
private static Customer GetCustomer(customerId) { | |
return ... | |
} | |
} | |
// now I can write the tests using this "StubRepository" implementation | |
public class StubRepository | |
{ | |
private readonly Customer customer; | |
public StubRepository(Customer customer) | |
{ | |
this.customer = customer; | |
DataRepository.getCustomer = customerId => customer; | |
} | |
} | |
// once I have the desired regression suite, I just refactor the internals properly to a similar | |
// implemnetation to Jason Gorman's |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment