You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
I have what seems to be a very similar issue to issue #3586, where TryValidateModel throws an Object reference error when unit tested. During normal operation with similar data being passed into the controller, the validation passes. Here is the controller method:
at Microsoft.AspNetCore.Mvc.ControllerBase.TryValidateModel(Object model, String prefix)
at LocationsAPI.Controllers.LocationsController.PartiallyUpdateLocation(Int32 id, JsonPatchDocument`1 patchDoc) in editedpath\src\LocationsAPI\Controllers\LocationsController.cs:line 138
at LocationsAPI.Tests.Unit.LocationsController_PatchLocationTests.PatchLocationReturnsNoContent() in editedpath\test\LocationsAPI.Tests.Unit\LocationsController_PatchLocationTests.cs:line 36
The text was updated successfully, but these errors were encountered:
You'll need to set controller.ObjectValidator when trying to test anything that calls TryValidateModel. I wonder if there's anything we can do to make this better.
@rynowak I was just having the same issue. while search for answer land me on this page. even adding the code below solve the problem. But it does not feel right that we need to do that in order to use TryValidateModel for unit testing.
It does not feel right because this piece of code is not within any of our testing logic. If we cannot improve upon this, we should at least mention this in the docs of TryValidateModel.
var objectValidator = new Mock<IObjectModelValidator>();
objectValidator.Setup(o => o.Validate(It.IsAny<ActionContext>(),
It.IsAny<ValidationStateDictionary>(),
It.IsAny<string>(),
It.IsAny<Object>()));
_sut.ObjectValidator = objectValidator.Object;
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I have what seems to be a very similar issue to issue #3586, where TryValidateModel throws an Object reference error when unit tested. During normal operation with similar data being passed into the controller, the validation passes. Here is the controller method:
...and here is the unit test implementation:
As well as the Automapper CollectionDefinition:
The stack trace:
The text was updated successfully, but these errors were encountered: