Answered by:
Modelstate.AddModelError Object reference not set to an instance of an object error

Question
-
User-501297529 posted
I get this error during unit testing.
Test::
public void EditItemStatus_Post_HandlesNotValid() { //arrange var createEditItemStatusViewModel = new CreateEditItemStatusViewModel() { Name = "Test", ColorId = 5, ItemStatusId = 4 }; _controller.ModelState.AddModelError("Error", "Error"); var result = _controller.EditItemStatus(createEditItemStatusViewModel) as ViewResult; //assert Assert.That(result, Is.Not.Null.And.InstanceOf<ViewResult>()); _mockService.VerifyAll(); _mockService .Verify(p => p.CreateEditStatus(It.IsAny<ItemStatus>()), Times.Never, "The Status could not be saved at this time"); }
Post Action:
public IActionResult EditDorItemStatus(CreateEditDorItemStatusViewModel StatusToEdit) { var availableColors = _service.GetAvailableColors().ToList(); availableColors.Add(new StatusColor() { ColorId = 0 }); StatusToEdit.AvailableColors = new SelectList(availableColors, "ColorId", "Name", availableColors.FirstOrDefault(c => c.ColorId == StatusToEdit.ColorId)?.ColorId); if (ModelState.IsValid) { var statusToBeEdited = _service.GetStatus(StatusToEdit.ItemStatusId); // Checks if item to be edit is null if (statusToBeEdited == null) { ModelState.AddModelError("StatusNotFound", "A matching item cannot be found to edit."); return View(StatusToEdit); } try { _mapper.Map(dorStatusToEdit, statusToBeEdited); if (_service.CreateEditStatus(statusToBeEdited)) { ViewData["SuccessMessage"] = "Changes to the Status have been successfully saved."; return View(StatusToEdit); } ModelState.AddModelError("UpdateFailure", "Submitted status contained invalid values. "); return View(StatusToEdit); } catch { ModelState.AddModelError("UpdateFailure", "The status could not be saved at this time."); return View(StatusToEdit); } } return View(StatusToEdit); }
Not sure what I'm missing and why it errors on that line.
Thursday, August 29, 2019 3:05 PM
Answers
-
User-501297529 posted
I figured this one out. I need to call the controller in the test method:
_controller = new Controller(_mockService.Object, _mockNewItemStatusMapper.Object);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 30, 2019 6:36 PM
All replies
-
User-474980206 posted
where did you construct the controller?
Thursday, August 29, 2019 3:57 PM -
User-501297529 posted
where did you construct the controller?
Not sure what you mean? Do you mean constructors?
Thursday, August 29, 2019 4:31 PM -
User475983607 posted
Where are you creating _controller?
_controller.ModelState.AddModelError("Error", "Error");
Testing controllers is cover at the following link which might help.
https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/testing?view=aspnetcore-2.2
Thursday, August 29, 2019 5:54 PM -
User-501297529 posted
I'm creating _controller in the Test class file. Not sure if this helps.
Unit test code:
internal class ControllerTest { #region Test Resources private Controller _controller; }
This is controller code:
public class Controller : AdminControllerBase { public Controller(IService Service) { _service = Service; } }
Thursday, August 29, 2019 6:05 PM -
User475983607 posted
You created a controller reference...
private Controller _controller;
but have not show where you "new" up a controller type. Which would look somehting like....
_controller = new ControllerUnderTest();
I recommend going through the link in my previous post which illustrates how to test controllers.
Thursday, August 29, 2019 6:48 PM -
User-501297529 posted
I figured this one out. I need to call the controller in the test method:
_controller = new Controller(_mockService.Object, _mockNewItemStatusMapper.Object);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 30, 2019 6:36 PM