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
If I have a class that inherits from ServicedComponent, TypeMock works as expected.

However, once I add the JIT activation attribute the test no longer works.

The approach I have used before with NMock is to use a Factory class that returns a mock object when unit testing and a real object in all other cases.

Is it possible to remove the need for the Factory class using TypeMock in this situation?
asked by chrisbartlett (680 points)

4 Answers

0 votes
Hi,

:?: About the first question.
What happens when you add the JITA Attribute?
Run the system with TypeMock Tracer and mock only your class. Do you see the instance in the Tracer? is it mocked?

You might need to use MockAll- this mocks both future and past instances.
You also have to use MockManager.Init() Before the first instance of your classes is created.


:?: About the second question.
Most times using TypeMock allows you to rid of factories that where created only for testing reasons. (You should of course use factories when the business requires it).
With TypeMock, you can mock the actual class without needing a factory at all. Just tell the framework that the next time a method of that class is called it should be mocked :-)
answered by scott (32k points)
0 votes
Scott, thank you for replying.

Performing my unit test with the JITA attibute I receive the very useful "The server threw an exception" message.

Tracer does show that my objects are mocked.

To help I have included the code in question:

This is the COM+ class marked with the JITA attibute. Removing it, and the test (see later) works.

[JustInTimeActivation(true)]
[Guid("932588BE-BBB6-4d9c-AD68-7155E6B6DB6B")]
public class SimpleDA : ServicedComponent
{
public SimpleDA()
{
}

public bool Execute()
{
ContextUtil.DeactivateOnReturn = true;
return true;
}
}

The data access class is called by the business object:

internal class SimpleBO
{
public SimpleBO()
{
}

public bool Execute()
{
bool isExecuted = false;

SimpleDA dataAccess = new SimpleDA();

try
{
isExecuted = dataAccess.Execute();
}
finally
{
//ServicedComponent.DisposeObject(dataAccess);
}

return isExecuted;
}
}

Finally, the test:

[Test]
public void TestExecute()
{
MockManager.Init();

Mock mockSimpleDA = MockManager.Mock(typeof(SimpleDA));
mockSimpleDA.ExpectAndReturn("Execute", false);

SimpleBO businessObject = new SimpleBO();

bool isExecuted = businessObject.Execute();

Assert.IsFalse(isExecuted, "Incorrect");

mockSimpleDA.Verify();
}


Regards,

Chris.
answered by chrisbartlett (680 points)
0 votes
Hi Chris,
We have tried to reproduce this error, but haven't really succeded in doing so.
We manged to mock the code example both with JITA and without.

Of course you have to register the COM+ object, before the test (I would put this into the test to make it self contained)

:?: What happens when you run the code without TypeMock? do you still get the error

Merry Christmas and a happy new year
answered by scott (32k points)
0 votes
Scott,

I was not registering the COM+ object before I ran the test - I was relying on dynamic registration.

:oops:

Once I added the registration to the test it worked.

:D

Thanks for your help in pointing me in the right direction - much appreciated.
answered by chrisbartlett (680 points)
...