Cropping with Microsoft Fakes and a static generic method

I need to flash a static universal method for unit test. However, I cannot get Fakes to create a strip object. Does anyone know how to do this?

In particular, I want to flash Newtonsoft JsonConvert.DeserializeObject <> ()

+4
source share
1 answer

For each return type that you expect, register a delegate as follows:

Using this code in Unit Test:

using (var context = ShimsContext.Create()) { ShimJsonConvert.DeserializeObjectOf1String<SomeJSonObject>(s => new SomeJSonObject() { Name = "Foo" }); SomeJSonObject o = ConsoleApplication3.Program.Deserialize(); Assert.IsNotNull(o); Assert.AreSame(o.Name, "Foo"); } 

And this verified code:

 return JsonConvert.DeserializeObject<SomeJSonObject>(""); 

It works as expected for me.

If necessary, also register other overloads. Therefore, if you use some other overloads, you must also register the appropriate delegates in Shim:

Other Overloads

how

 ShimJsonConvert.DeserializeObjectOf1String<SomeJSonObject>(s => new SomeJSonObject() { Name = "Foo" }); ShimJsonConvert.DeserializeObjectOf1StringJsonConverterArray((s, convertors) => new SomeJSonObject() {Name = "Bar"}); ShimJsonConvert.DeserializeObjectOf1StringJsonSerializerSettings((s, settings) => new SomeJSonObject() { Name = "Bar" }); 
+5
source

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


All Articles