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
- Assert that the exception is thrown
- Assert that an existing
SomeClass
is returned - Assert that a new
MyClass
is returned
Do 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.?