I'm using NJsonSchema to validate JSON input.
I have a small class that takes a collection of ValidationError
objects and creates more user friendly error messages using the contents of each validation error.
I want to be able to write unit tests for this class, however I have came across a problem. One of the message handlers within my class has the responsibility of handling NotInEnumeration
errors, and for this it uses the Enumeration
property within the JsonSchema4
object held within the ValidationError and creates a nicely formatted error message.
When writing a test for this particular handler I discovered that the following is illegal:
JsonSchema4 enumSchema = new JsonSchema4();
enumSchema.Enumeration = new List<object>{ "A", "B", "C" };
This is because the Enumeration property has an internal setter.
I need to be able to set the enumeration of a validation error as the object needs to be passed through to the ValidationError's constructor, which is then read later on by my handler, as below.
private string NotInEnumerationHandler(ValidationError error)
{
var userFriendlyErrorString = "Answer must be within range: ";
var enumString = "[" + string.Join<object>(", ", error.Schema.Enumeration) + "]";
userFriendlyErrorString += enumString;
return userFriendlyErrorString;
}
I cannot mock the JsonSchema4 object using moq as moq doesn't allow mocking of non-virtual methods.
Essentially the details aren't really important, but I'd like to know if there's any way of setting an internal setter so that I can test this particular method within my class.