From 9f69ef5707bd709ac47b5eaf1c31b1a6116e9ce4 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Fri, 11 Mar 2016 11:47:12 -0500 Subject: [PATCH] Initial progress --- aspnet/mvc/controllers/testing.rst | 33 +++++++++++++++++++++++++----- aspnet/testing/index.rst | 1 + 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/aspnet/mvc/controllers/testing.rst b/aspnet/mvc/controllers/testing.rst index cd6380578a..f94dc43a62 100644 --- a/aspnet/mvc/controllers/testing.rst +++ b/aspnet/mvc/controllers/testing.rst @@ -1,8 +1,31 @@ -.. include:: /../common/stub-topic.txt +Testing Controller Logic +======================== +By `Steve Smith`_ -|stub-icon| Testing Controller Logic -==================================== +Controllers in ASP.NET MVC apps should be small and focused on user-interface concerns. Provided this recommendation is followed, testing your app's controller logic should be fairly straightforward. -.. include:: /../common/stub-notice.txt +.. contents:: Sections + :local: + :depth: 1 + +`View sample files `_ -.. _issue: https://github.com/aspnet/Docs/issues/123 +What is Controller Logic +------------------------ +*Controllers* define groups of *actions*. The framework could have been designed around isolated actions, but grouping them using controllers often useful for :doc:`routing `, applying :doc:`filters `, and :doc:`injecting common dependencies `. + +:doc:`Learn more about controllers and actions `. + +Controller logic should be minimal and should not be focused on business logic or infrastructure concerns like data access. When testing controller logic, avoid testing the framework as much as possible. You should test how the controller *behaves* based on valid or invalid inputs, and what kind of response it provides based on the result of some business operation it performs. + +Typical controller responsibilities: + - Verify ``ModelState.IsValid`` + - Return an error response if ``ModelState`` is invalid + - Retrieve a business entity from persistence + - Perform an action on the business entity + - Save the business entity to persistence + - Return an appropriate ``IActionResult`` + +Unit Testing +------------ +:doc:`Unit testing ` involves testing a part of an app in isolation from its infrastructure and dependencies. When unit testing controller logic, only the contents of a single action should be tested. \ No newline at end of file diff --git a/aspnet/testing/index.rst b/aspnet/testing/index.rst index 86f6306a32..c2ea1f5367 100644 --- a/aspnet/testing/index.rst +++ b/aspnet/testing/index.rst @@ -6,6 +6,7 @@ Testing unit-testing integration-testing + ../mvc/controllers/testing