Hi lior,
finally I got back to that issue.
Below is sample code that shows the issue.
ObjectUnderTest is the class I want to test.
ThrowExcpOnToString is the class that will throw an exception if ToString is called.
ObjectNotPartOfTest is another class that is mocked so that it is not part of the test.
The test "Test_MockAsParam_Fails_Because_of_CheckArguements" fails because ToString is called when TypeMock tries to verify the values. My feeling is that TypeMock should not do that for all parameters that have been created using RecorderManager.CreateMockedObject because those objects are no real objects...
Otherwise, I have to switch CheckArguments on and off in a test if the ToString method of a mocked object is doing something that needs filesystem, DB or any other things that I do not have in my test.
Step
using System;
using NUnit.Framework;
using TypeMock;
namespace TypeMockExamples
{
[TestFixture]
public class MockObjectAsParameter
{
public class ThrowExcpOnToString
{
public override string ToString()
{
throw new Exception("ToString called");
}
}
public class ObjectNotPartOfTest
{
public void MethodA(ThrowExcpOnToString obj)
{
}
}
public class ObjectUnderTest
{
private ObjectNotPartOfTest _a;
private ThrowExcpOnToString obj;
public ObjectUnderTest(ObjectNotPartOfTest a, ThrowExcpOnToString obj)
{
_a = a;
this.obj = obj;
}
public void MethodTested()
{
_a.MethodA(obj);
}
}
[Test]
public void Test_MockAsParam_Fails_Because_of_CheckArguements()
{
ObjectNotPartOfTest a = RecorderManager.CreateMockedObject<ObjectNotPartOfTest>();
ThrowExcpOnToString obj = RecorderManager.CreateMockedObject<ThrowExcpOnToString>();
ObjectUnderTest myTestObject = new ObjectUnderTest(a,obj);
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
recorder.DefaultBehavior.Strict = StrictFlags.AllMethods;
recorder.DefaultBehavior.CheckArguments();
a.MethodA(obj);
}
myTestObject.MethodTested();
}
[Test]
public void Test_MockAsParam_Succeeds_Because_of_CheckArguements_Disabled()
{
ObjectNotPartOfTest a = RecorderManager.CreateMockedObject<ObjectNotPartOfTest>();
ThrowExcpOnToString obj = RecorderManager.CreateMockedObject<ThrowExcpOnToString>();
ObjectUnderTest myTestObject = new ObjectUnderTest(a, obj);
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
recorder.DefaultBehavior.Strict = StrictFlags.AllMethods;
a.MethodA(obj);
}
myTestObject.MethodTested();
}
}
}