I'm learning and trying TDD recently, and I constantly encounter a situation when I need to ditch my current test because it's too broad. I don't know how to deal with it. To be more specific, let's suppose I have a class User
, which contains a method getInfo
. To follow TDD, I need to write my test first:
@Test
public void test_get_info() { ... }
Then implement my method:
class User {
public UserInfo getInfo() { ... }
}
When I'm implementing the method, I suddenly find out that there are two kinds of users, one is Employee, the other is manually-registered user, one test is not enough to cover both. To get info for both kinds of users, I need to create two more classes:
class Employee extends User {
public UserInfo getInfo() { ... }
}
class RegisteredUser extends User {
public UserInfo getInfo() { ... }
}
Before implementing it, I have to write two tests one by one for Employee.getInfo
and RegisteredUser.getInfo
. But suppose when I've finished writing the test for Employee.getInfo
and try to implement it, I find out that UserInfo
contains user name and user address, both have complicated logic and deserve a separate test case. Thus, before implementing Employee.getInfo
, I need to write tests for UserInfoFetcher.getUserName
and UserInfoFetcher.getUserAddress
...
Have you seen the problem? To pass the first test, I need to write multiple other tests and pass them before the first one. It doesn't seem like TDD, which usually demands one test each time. How do you solve the problem?