- May 27, 2002
- 12,648
- 201
- 106
I have a class I am trying to write a unit test for, and I need to write a Shim to override the default constructor behavior, MS documentation suggests this is possible. but I can't seem to make it work.
This case has been simplified for the sake of brevity...Please help!
1st unit test works perfectly.
Now I need one to remove the dependency through s Shim Injection, and this is the one that is failing.
Assert.Are equal failed. Expected <MyString has been set to something different.> Actual: <(Null)>
not sure why this is failing, im following all the examples I can find.
https://msdn.microsoft.com/en-us/library/hh549176.aspx
https://adamprescott.net/2012/08/21/a-shim-ple-tutorial-with-microsoft-fakes/
This case has been simplified for the sake of brevity...Please help!
Code:
public class MyClass
{
[INDENT]private String MyString;
private String MyStringB;
public MyClass()
{
[INDENT]MyString = "MyString has been set in declaration.";
MyStringB = SetMyString();[/INDENT]
}
private String SetMyString()
{ return "My String has been set by the function call."; }[/INDENT]
}
1st unit test works perfectly.
Code:
[TestClass]
public MyTestClass
{
[INDENT]String DesiredOutCome = "My String has been set by the function call.";
[TestMethod]
public void TestMyClassConstructor()
{
MyClass MyObject = new MyClass();
PrivateObject obj = new PrivateObject(MyObject);
var MyOutCome = obj.GetFieldOrProperty("MyStringB");
Assert.AreEqual(MyOutCome, DesiredOutCome);
}[/INDENT]
}
Now I need one to remove the dependency through s Shim Injection, and this is the one that is failing.
Code:
[TestClass]
public MyTestClass
{
[INDENT]String NewDesiredOutCome = "MyString has been set to something different.";
[/INDENT]
[TestMethod]
public void TestMyClassConstructorWithShim()
{
using (ShimsContext.Create())
{
MyNameSpace.Fakes.ShimMyClass.Constructor = (@this) =>
{
var shim = new ShimMyClass(@this)
{
SetMyString = () => { return "MyString has been set to something different."; }
};
};
MyClass MyObject = new MyClass();
PrivateObject obj = new PrivateObject(MyObject);
var MyOutCome = obj.GetFieldOrProperty("MyStringB");
Assert.AreEqual(MyOutCome, NewDesiredOutCome);
};
}
}
Assert.Are equal failed. Expected <MyString has been set to something different.> Actual: <(Null)>
not sure why this is failing, im following all the examples I can find.
https://msdn.microsoft.com/en-us/library/hh549176.aspx
https://adamprescott.net/2012/08/21/a-shim-ple-tutorial-with-microsoft-fakes/