I made simple windows form with 2 button on it, button 1 is to prepare and test, button 2 is to act.
problem 1.
when I click on button 1 and then button 2, it will fake the method, but the passed parameter is not correct. it's the value I used when I was in preparing phase, not the actual value passed to the method, when I do it without using reference parameter passing, everything will be fine. but in my real scenario I have to use reference parameter passing.
problem 2.
when I click button 2 first and then button 1, it will throw an exception that contains:
*** No method calls found in recording block. Please check:
* Are you trying to fake a field instead of a property?
* Are you are trying to fake mscorlib type?
It means that if original method is called once before faking, we can not fake it anymore.
I can handle problem 2 by prevention of calling original method before faking, but I don't know what to do with problem 1.
here is my code:
namespace TypeMocTestAPP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[TestMethod]
private void button1_Click(object sender, EventArgs e)
{
//Prepare
var fake = Isolate.Fake.Instance<TestClass>();
DateEx dateEx = new DateEx { Day = 1, Month = 2, Year = 2000 };
Isolate.WhenCalled(() => fake.DateToString(ref dateEx)).DoInstead(context =>
{
string param0 = ((DateEx)context.Parameters[0]).Year.ToString();
return "Fake is Called, Year is:" + " " + param0;
}
);
Isolate.Swap.AllInstances<TestClass>().With(fake);
}
private void button2_Click(object sender, EventArgs e)
{
//ACT
TestClass testClass = new TestClass();
DateEx refDate = new DateEx() { Year = 1999, Month = 11, Day = 1 };
MessageBox.Show(testClass.DateToString(ref refDate));
}
}
public struct DateEx
{
public int Year;
public int Month;
public int Day;
}
public class TestClass
{
public string DateToString(ref DateEx dateEx)
{
return "Original is Called, Year is: " + " " + dateEx.Year.ToString();
}
}
}