Ah, I see what you want to do...
.. but I don't know how to do that. Maybe the TypeMock folks can help out with that. I tried using the MockMethodCalled event and it didn't work for the constructor.
If it's new code you're writing, I might encourage you to redesign slightly for a better testability. I'm not a big fan of the "design for testability" thing, but you might be better able to do what you're doing with a private method you could mock. Like:
public class MyClass
{
private string m_A;
private string m_B;
public MyClass(string a, string b)
{
m_A = a;
m_B = b;
}
public string ConcatValues()
{
if (m_A == null && m_B == null)
{
return null;
}
return m_A + m_B;
}
}
public class ConsumerClass
{
private MyClass GetMyClassInstance()
{
return new MyClass("a", "b");
}
public string GetMyClassValue()
{
MyClass instance = this.GetMyClassInstance();
return instance.ConcatValues();
}
}
[TestFixture]
[VerifyMocks]
public class ConsumerClassTest
{
[Test]
public void ChangeCtorParams()
{
MyClass substitute = new MyClass("x", "y");
Mock<ConsumerClass> mock = MockManager.Mock<ConsumerClass>();
mock.ExpectAndReturn("GetMyClassInstance", substitute);
ConsumerClass consumer = new ConsumerClass();
Assert.AreEqual("xy", consumer.GetMyClassValue());
}
}
Note how I'm mocking the private GetMyClassInstance "factory" method and instead returning a constructed instance with my desired state.
Generally, rather than mock the constructor parameters, I end up mocking the call to whatever public-facing method someone is interfacing with (like "ConcatValues"). That's what I did in the original examples.
Of course, you could fall back to good old fashioned reflection:
[Test]
public void ChangeCtorParams()
{
MyClass instance = new MyClass("a", "b");
typeof(MyClass).GetField("m_A", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(instance, "x");
typeof(MyClass).GetField("m_B", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(instance, "y");
Assert.AreEqual("xy", instance.ConcatValues());
}
But then you're really getting into brittle-test territory, where the test knows far too much about the inner workings of the class it's testing.