chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,
for my tests, I normally have the parameter checking turned on.
( recorder.DefaultBehavior.CheckArguments(); )
I also normally turn on
recorder.DefaultBehavior.Strict = StrictFlags.AllMethods;

It seems like that if a natural mock is passed into a method, that typemock is trying to determine the value of the natural mock. (I see that the ToString() method of my mocked object is called and that it is throwing an exception. Sometime I also get the error that ToString was called but not expected). If I turn off the parameter checking for the method calls where the mock is passed in, my tests work.

Is there a way to tell TypeMock to do parameter checking for all parameters, except of parameters that are mocked? I use following call to create the mock

writer = RecorderManager.CreateMockedObject<IMyWriterClient>();
Can I somehow tell the recorder manager not to check the value of writer if it is passed into a method?

Thanks
Step
asked by Step (3.4k points)

4 Answers

0 votes
Hi Step,

I don't think that currently we have such an ability.
However I'm not sure I fully understood your issue.

would it be possible to post a simple test that will describe the problem?

I would like to fully understand the issue and see if we can find some mechanism to by pass the issue.
answered by lior (13.2k points)
0 votes
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();

}
}
}
answered by Step (3.4k points)
0 votes
Hi Step,

Thanks for the code.
I ran it with Typemock Isolator 4.2.3, and both tests pass. So I couldn't reproduce it here.
Assuming you can reproduce this, I'm going to send an email for logs, with instructions, so we can continue to investigate it.

Hoping to solve this quickly.
answered by gilz (14.5k points)
0 votes
Hi everyone,

Just to close this - The problem occured when using version 4.0.3. After upgrading to 4.2.3 the problem was resolved.
answered by gilz (14.5k points)
...