How can you test that an action method you have like so will return the correct view because Viewname is an empty string? Should I even bother testing this? I'm not sure how many unit tests to do, I'm thinking you could create a lot of unit tests!
public ActionResult Index()
{
return View();
}
[TestMethod]
public void DetermineIndexReturnsCorrectView()
{
HomeController controller = new HomeController();
ViewResult result = controller.Index() as ViewResult;
//****result.ViewName is empty!!!!***//
Assert.AreEqual("Index", result.ViewName);
}
Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.
Mutation score indicator is calculated as the percentage of faults (mutants) detected by the test suite. This score can be used to measure the effectiveness of a test set in terms of its ability to catch faults. A higher mutation score indicates higher effectiveness of test suite in detecting the faults.
The assert section ensures that the code behaves as expected. Assertions replace us humans in checking that the software does what it should. They express requirements that the unit under test is expected to meet. Now, often one can write slightly different assertions to capture a given requirement.
The purpose of a unit test in software engineering is to verify the behavior of a relatively small piece of software, independently from other parts. Unit tests are narrow in scope, and allow us to cover all cases, ensuring that every single part works correctly.
Test the type of the result.
//Act
var result = controller.Create();
//Assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
Then write separate tests for RedirectToRouteResult cases and handle exception cases as well and you're set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With