Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Isolate (detour) constructor of class with Microsoft Fakes Shim?

Is there a possibility to isolate / replace an constructor of a class with Microsoft Fakes?

In found an example for Mole (antecessor of Fakes): http://thecurlybrace.blogspot.co.at/2011/11/how-do-i-detour-mole-type-constructor.html

I tried constructs like this

ShimStreamReader.Constructor = @this => ShimStreamReader.ConstructorString(@this, "Test");

but it says the get accessor is missing. To clarify it would be nice to replace something like

new StreamReader("filename")

with static input like this

new StreamReader(new MemoryStream(Encoding.Default.GetBytes("33\r\n1\r\n16\r\n5\r\n7")))

so that i do not have to mock Read, ReadLine, etc.

like image 322
Dresel Avatar asked Jul 24 '12 13:07

Dresel


2 Answers

using (ShimsContext.Create())
{
    ShimStreamReader.ConstructorString = 
        delegate(StreamReader @this, string @string)
        {
            var shim = new ShimStreamReader(@this);
            shim.Read = () => 42;
        };

    var target = new StreamReader("MeaningOfLife.txt");
    Assert.AreEqual(42, target.Read());
}
like image 196
Oleg Sych Avatar answered Nov 02 '22 04:11

Oleg Sych


It is possible to replace and call a different constructor via reflection:

"Hence, when you are replacing a constructor you should do all your initialization in your constructor. For StreamReader constructor is may not be possible as you don't know how object is internally initialized by original constructor and you don't have access to private members of object.

So, we can use reflection to call in original constructor with to initialized the object and then use it." - See Vikram Agrawals answer on http://social.msdn.microsoft.com

So the code regarding to my question would look like this:

ShimStreamReader.ConstructorString = (@this, value) =>
{
    ConstructorInfo constructor = typeof (StreamReader).GetConstructor(new[] {typeof (Stream)});
    constructor.Invoke(@this, new object[] {new MemoryStream(Encoding.Default.GetBytes("33\r\n1\r\n16\r\n5\r\n7"))});
};
like image 4
Dresel Avatar answered Nov 02 '22 04:11

Dresel