Question: I have a simple Unit test too. Im just studing. namespace UnitTest { [TestClass] public class HomeController { [TestMethod] public void Index_NoInputs_ReturnsDefaultViewResult() { // Arrange

I have a simple Unit test too. Im just studing.

namespace UnitTest { [TestClass] public class HomeController { [TestMethod] public void Index_NoInputs_ReturnsDefaultViewResult() { // Arrange HomeController controller = new HomeController(); // Act ViewResult result = Index() as ViewResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("", result.ViewName); Assert.IsNull(result.Model); } } }

The index is underlined at the Act

My HomeController.cs

public class HomeController : Controller { private readonly IEmployeeRepository _employeeRepository; public HomeController(IEmployeeRepository employeeRepository) { _employeeRepository = employeeRepository; } public IActionResult Index() { return View(); } [HttpGet] public IActionResult GetAllEmployee() { var allEmployee = _employeeRepository.AllEmployee(); var j = Json(allEmployee); return j; } [HttpGet] public IActionResult GetEmployeeDetails(int id) { var employee = _employeeRepository.Get(id); return Json(employee); } }

Ive also tried it this way too. But it still not okay.

namespace UnitTest { [TestClass] public class HomeControllerTests { [TestMethod] public void Index_NoInputs_ReturnsDefaultViewResult() { IEmployeeRepository repository = .......; HomeController controller = new HomeController(repository); // Act ViewResult result = controller.Index() as ViewResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("", result.ViewName); Assert.IsNull(result.Model); } } }

I tried figure out what comes after the

IEmployeeRepository repository = ...

But it just did not want work out.

Please show me the full test coding and explain it too. .Im just studing. Thanks.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!