To cut the managing cost for the controller/api, we creating test cases for those controller/api which help us understand the impact at the time of implementation/modification.
If you are already familiar with test cases for the class then here nothing new or separate concept to test the controller action because the action is a public method in the class inheriting Controller class. The only difference you may get the response type.
Remember here I am explaining you, how to create test cases for an action in controller whose complexity is zero.
There are two ways you can create test cases project
1. Use the visual studio built-in Unit Test Project template.
2. Use the Library project and in reference add the UnitTestFramework (Microsoft.VisualStudio.QualityTools.UnitTestFramework).
Below is the basic code snippet of HomeController which contain an action Index which in response returning a string.
And the below code snippet of Test Cases for above Index action.
In test method, you can see it's divided into three parts
1. Arrange - declare or initialize all instance/variables which required for test cases.
2. Act - call the method for which test method creating.
3. Assert - verify the expected output using assert.
How to Run Test Cases?
In the menu select the Test -> Run-> All Tests.
Short key Ctrl+R, A
Thanks for giving your valuable time, if you have any query please comment below.
If you are already familiar with test cases for the class then here nothing new or separate concept to test the controller action because the action is a public method in the class inheriting Controller class. The only difference you may get the response type.
Remember here I am explaining you, how to create test cases for an action in controller whose complexity is zero.
There are two ways you can create test cases project
1. Use the visual studio built-in Unit Test Project template.
2. Use the Library project and in reference add the UnitTestFramework (Microsoft.VisualStudio.QualityTools.UnitTestFramework).
Below is the basic code snippet of HomeController which contain an action Index which in response returning a string.
public class HomeController : Controller
{
public string Index()
{
return "Hello from Index action in Home controller";
}
}
In above snippets you can see the action returning a string and the method/action complexity is zero.And the below code snippet of Test Cases for above Index action.
[TestClass]
public class HomeControllerTest
{
[TestMethod]
public void ShouldReturnHelloMsgFromIndexAction()
{
// Arrange
string expected = "Hello from Index action in Home controller";
var ctrl = new HomeController();
// Act
var result = ctrl.Index();
// Assert
Assert.AreEqual(expected, result);
}
}
You can see the class 'HomeControllerTest' decorated with TestClass
attribute which tell the compiler the class contain the test method(s).
And the method 'ShouldReturnStringFromIndexAction' decorated with
attribute 'TestMethod' which identify the test methods.In test method, you can see it's divided into three parts
1. Arrange - declare or initialize all instance/variables which required for test cases.
2. Act - call the method for which test method creating.
3. Assert - verify the expected output using assert.
How to Run Test Cases?
In the menu select the Test -> Run-> All Tests.
Short key Ctrl+R, A
Thanks for giving your valuable time, if you have any query please comment below.