JsonSerialization exception when creating JToken from Mock Object

I have the following line of code that I am trying to do in unit testing with a mockery of a specific class that makes internal calls to databases (and not according to my design):

var configuration = config.SelectToken("config").ToObject<Configuration>();

I am trying to mock the configuration class and add it, as well as the name of the "config" property to the JObject instance:

var mockConfig= new Mock<Configuration>();

var jToken = JToken.FromObject(mockConfig);

dynamic jsonObject = new JObject
{
    {"config", jToken }
};

When I try to create a JToken from a mock object, I get the following exception:

Newtonsoft.Json.JsonSerializationException: A member named "Mock" already exists in "Castle.Proxies.ConfigurationProxy". Use JsonPropertyAttribute to specify a different name.

+4
source share

Source: https://habr.com/ru/post/1611671/


All Articles