<

Question 117


4.3.1 Question

Multiple Choice

You are under contract with a large company that is starting to implement a TDD approach. As part of the long-term support for this effort, the company needs you to complete several unit tests. As you review the current code base, you find good tests for the model, but no tests for anything other than the model. Which of the following is the best approach to complete the unit tests?

  • A. Create a new directory in the unit test project for the controller- and action-specific tests. Create a unit test file for each controller. Inside that file, have one or more tests for only the controller action methods.
  • B. Add a new file to the unit test project called ControllerTests. Put all tests for all the controllers and the actions in the file.
  • C. Create a new directory in the unit test project for the controller- and action-specific tests. Create a unit test file for each action you are going to test.
  • D. Create a new directory in the unit test project for the controller- and action-specific tests. Create a unit test file for each controller. Inside that file have one or more tests for all methods in the controller, regardless of whether they are an action or not.

Answer:

D
Explanation
A. Incorrect: There might very well be nonaction methods in a controller. Those methods should be tested as well.
B. Incorrect: You should provide much more separation of your tests than using a single file for every unit test that applies to a controller.
C. Incorrect: This is too much of a breakdown. The best relationship between conĀ­trollers and the applicable unit tests is usually 1 to 1. However, a controller with a large number of methods working within the controller will not meet the 1-to-1 ratio. You should test nonaction methods in the controller as well.
D. Correct: This solution provides for testing actions and nonactions as well as a good split of the tests per file.