While following test driven development, I came across a function that I need to write which is similar to the following:
public MyClass DoSomething(string value)
{
SomeClass existCheck = repository.Get(value);
if(existCheck == null)
throw new InvalidOperationException("SomeClass must exist to do something");
if(existCheck.MyClass != null)
return existCheck.MyClass;
MyClass myClass = new MyClass()
{
// create object
};
return myClass;
}
Using TDD, I would need to write seperate tests to
SomeClass
is returnedMyClass
is returnedDo I write all
three tests first then code them or do I write each test, then code the functionality in the function required for the test to pass and then write the next test and the code the functionality etc.?
I've always believed that with TDD you should take baby steps, with the Red-light Green-light approach. I would proceed in the following steps:
Generally, I find you can progress with a test as the method proceeds, and then copy the test each time you reach a branch, tailoring each test to each differing path.