Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Introducing a simple concrete factory to ReallyHardToTest
// This is an example class illustrating some common patterns
// that negatively impact testability
//
// In this round, we are introducing a factory
// for SomeModel so we do not have to use
// the new operator.
public class ReallyHardToTest{
SomeModel model;
IPathWrapper pathWrapper;
public ReallyHardToTest(ISomeOtherDependency otherDependency, IPathWrapper pathWrapper, ISomeModelFactory someModelFactory){
this.pathWrapper = pathWrapper;
model = someModelFactory.Create();
otherDependency.DoSomething();
}
public void SetFilename(string fileName){
model.Filename = pathWrapper.GetFileName(fileName);
}
}
public interface ISomeModelFactory{
SomeModel Create();
}
public class SomeModelFactory : ISomeModelFactory{
SomeModel Create(){
return new SomeModel;
}
}
public class SomeModel{
public string Filename{get;set;}
}
public interface ISomeOtherDependency{
void DoSomething();
}
public class SomeOtherDependency : ISomeOtherDependency {
public void DoSomething(){
// Just here for illustration
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.