Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Test to determine that Action is returning correct View

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);
}
like image 490
Jon Avatar asked Jun 28 '11 20:06

Jon


People also ask

What is a unit test when should it be done?

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.

How do you measure unit testing effectiveness?

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.

What is unit test assertions?

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.

What does a unit test attempt to achieve?

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.


1 Answers

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.

like image 191
Khepri Avatar answered Sep 28 '22 09:09

Khepri