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
I am having some unexpected behavior while trying to test a method by mocking a Form subclass.

Since my method creates a new Form then calls its ShowDialog method I wanted to mock the form and fake the data that is returned by the dialog. To Mock the dialog (since it is created in the method being tested) I use:

                Mock FormM = MockManager.MockAll (typeof (LegacyTablePurgeForm), Constructor.Mocked);


I then mock the methods I want to mock and execute, my test code and then call MockManager.Verify.

Now the test passes and all is well (almost). I am finding that after the test has completed running (i.e. after the MockManager has been verified) an exception is thrown when my Mocked form is disposed via the finalize of the Component.

Here is the stack trace from my Nant task (it is Nant that catches the exception and has to die).

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at DevExpress.XtraEditors.XtraForm.Dispose(Boolean disposing)
at Vigis.Database.LegacyTablePurgeForm.Dispose(Boolean disposing) in c:devigisisitDatabaseLegacyTablePurgeForm
.Designer.cs:line 16
at System.ComponentModel.Component.Finalize()

If the constructor of my form is mocked, how can Component.Finalize () even exist... are the base constructors being called when the constructor is mocked?

When I use the object tracer, it shows that only one mocked instance of my Form ever exists (but, keep in mind that exception is thrown after the mock object goes out of scope, so I don't think the trace can see the call to the dispose method).

I don't think I can manually dispose the form, because it is mocked, so calling dispose does nothing.

How can I clean this up?

Thanks in advance for any help you can give me.

ds.
asked by sellingerd (5.7k points)

1 Answer

0 votes
Hi,
It would help to see your code.

Here is one way that might help:
using(LegacyTablePurgeForm testedFrom = new LegacyTablePurgeForm())
{
   ... test here
}
MockManager.Verify();
answered by scott (32k points)
...