Answered step by step
Verified Expert Solution
Question
1 Approved Answer
UnitTest for Controller My HomeController.cs namespace Chegg.InterviewDec2023.UI.Controllers; public class HomeController : Controller { private readonly IStaffRepository _staffRepository; public HomeController(IStaffRepository staffRepository) { _staffRepository = staffRepository; }
UnitTest for Controller
My HomeController.cs
namespace Chegg.InterviewDec2023.UI.Controllers; public class HomeController : Controller { private readonly IStaffRepository _staffRepository; public HomeController(IStaffRepository staffRepository) { _staffRepository = staffRepository; } public IActionResult Index() { return View(); } [HttpGet] public IActionResult GetAllStaff() { var allStaff = _staffRepository.AllStaff(); var j = Json(allStaff); return j; } [HttpGet] public IActionResult GetStaffDetails(int id) { var staff = _staffRepository.Get(id); return Json(staff); } }
My UnitTest
namespace TestProject1 { [TestClass] public class HomeControllerTest { private Mock _staffRepository; private Fixture _fixture; private HomeController _controller; public HomeControllerTest() { _fixture = new Fixture(); _staffRepository = new Mock(); } [TestMethod] public async Task Get_Staff_ReturnOK() { var staffList = _fixture.CreateMany(3).ToList(); _staffRepository.Setup(repo => repo.AllStaff()).Returns(staffList); _controller = new HomeController(_staffRepository.Object); var result = await _controller.GetStaffDetails(); var obj = result as ObjectResult; Assert.AreEqual(result); } } }
Something not good at the public HomeControllerTest()
row with the HomeControllerTest. Also at the await _controller.GetAllStaff();
part.
Could you please help me what am I doing wrong and she the right code and explain it to me please.
Thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started