Last active
September 10, 2015 04:02
-
-
Save up1/84227f45f0c48c12acd1 to your computer and use it in GitHub Desktop.
Demo Dependency Injection
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
public class Example { | |
private DatabaseThingie myDatabase; | |
public Example() { | |
myDatabase = new DatabaseThingie(); | |
} | |
public void doStuff() { | |
myDatabase.GetData(); | |
} | |
} |
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
public class Example { | |
private DatabaseThingie myDatabase; | |
public Example() { | |
myDatabase = new DatabaseThingie(); | |
} | |
public Example(DatabaseThingie useThisDatabaseInstead) { | |
myDatabase = useThisDatabaseInstead; | |
} | |
public void DoStuff() { | |
myDatabase.GetData(); | |
} | |
} |
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
public class ExampleTest { | |
TestDoStuff() { | |
MockDatabase mockDatabase = new MockDatabase(); | |
// MockDatabase is a subclass of DatabaseThingie, so we can | |
// "inject" it here: | |
Example example = new Example(mockDatabase); | |
example.DoStuff(); | |
mockDatabase.AssertGetDataWasCalled(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment