Say I have the following code, which cannot be modified:
class A {
public doSomething() {
// lots of business logic
data = obtainData();
// lots of other business logic which depends on data
}
private obtainData() {
// connect to the database
// send web requests
// send manned missions to the moon
// process the obtained data
return data;
}
}
What are the best practices of testing such a code? I want to make sure doSomething() does what it should do, but I want to provide it with known data instead of running the code inside obtainData().
Have a look at Moles to help you with brownfield testing. Moles in C# allow you provide your own implementations for private code and other people’s code.
When you have a good set of tests that cover this mess, please re-factor to save humanity.
If the originator of the spaghetti code is within reach, please smack him for me, why don’t ya?
For best practices, read Uncle Bob’s Clean Code http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
To summarize: if code under test has external dependencies, make them explicit and externally-configurable.