We have been developing REST APIs in .Net core 3.1 and using MS TEST - Test Project in Visual Studio 2019 for unit testing.
Now we have a hard rule to follow Test Driven Development. In theory I can understand that TDD will involve writing tests to implement a requirement and continuously iterate through RED GREEN and REFACTOR cycles.
Now my question is, if I have a endpoint which will get all books according to author id from DB, like following endpoint
httpget : api/boooks/authors/1006
which will return a response, what all cases/ test methods should I write in the Test class of MSTest project, so that I can say that, I have followed Test Driven Development ?
Before TDD, I will write a TestMethod for the Service method which will call the Database layer and get the author information from SQL DB. SO only 1 test method with different input to mock, will be written.
I am very new to TDD hence the question. Any comment is helpful
CodePudding user response:
If you're in doubt about how many test cases you need to write, you may consider using the Devil's Advocate technique to critique your test code.
If, for example, you write a single unit test (pseudocode):
Initialise System Under Test (SUT)
Call api/boooks/authors/1006
Assert that the response is, say, [Foo, Bar]
Then, according to the red-green-refactor checklist and the Devil's Advocate technique, the implementation should be the simplest thing that could possibly work. What would that be?
return [Foo, Bar];
Unconditionally returning a hard-coded value is not the implementation you have in mind. This means that you have at least one more test case to write.
Another heuristic to keep in mind is that there should be at least as many test cases as the cyclomatic complexity of the implementation, and most likely more.