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
0 votes
Hi,

We have a large library of unit tests which used to run prior to migration from Visual Studio 2005 to 2008.

After the migration some unit tests have started failing and I have traced at least some of the failures down to a failure in mocking System.Diagnostics.Process.

The following code snippet simplifies and illustrates the issue.

    [TestMethod]
    public void MainWindowHandleTest()
    {
      MockManager.MockAll<Process>().ExpectGetAlways("MainWindowHandle", (IntPtr)1234);
      Assert.AreEqual((IntPtr)1234, Process.GetCurrentProcess().MainWindowHandle);
    }


In the above example I would expect that the assertion passes, but in fact the assertion fails.

I have logged this as a bug because it used to work under VS2005.

My environment is as follows.

OS: Windows XP Pro SP2, fully updated.
TypeMock: v4.1.0.0, Full installation with .NET 1.1 support subsequently removed.
Development: Visual Studio 2008 (v9.0.21022.8 RTM) targeting the .NET framework v3.5

Regards,

Nicklas.
asked by Ency (1.6k points)

7 Answers

0 votes
Hi
This looks like like a bug.
You can use a different runner like TestDriven.NET which seems to run fine
In any case I suggest you'll try the latest version (4.2.2) and see if still happens.
answered by ohad (35.4k points)
0 votes
Thanks Ohad,

I'll give 4.2.2 a try when I get a chance and let you know.
answered by Ency (1.6k points)
0 votes
Has there been any updates to the System.Diagnostics.Process issue?

Reason I ask is because I am trying to mock the Start method in the process class with the following code:

Mock mockSystemDiagnosticsProcess =
            MockManager.Mock(typeof(System.Diagnostics.Process),Constructor.NotMocked);
mockSystemDiagnosticsProcess.ExpectAndReturn("Start", new System.Diagnostics.Process());


Enviornment follows:
Visual Stuidio 2003 targeting .net 1.1
Windows XP Pro
Version 4.2.4.0 Enterprise Edition

There very well could be something i overlooked and any help would be greatly appreciated.

Thank you.
answered by Deadeye (220 points)
0 votes
Hi,

I'd like a bit more explanation on what you are trying to do.

The example you wrote was creating a mock for the Process class.
Then you set an expectation on the Start method. But the Start method returns a bool. So the ExpectAndReturn method parameter is incorrect. If you are trying just to mock the Start method use something like this:

Mock mockSystemDiagnosticsProcess = MockManager.Mock(typeof(System.Diagnostics.Process), Constructor.NotMocked); 
mockSystemDiagnosticsProcess.ExpectAndReturn("Start", true);

System.Diagnostics.Process proc = new System.Diagnostics.Process();
Assert.IsTrue(proc.Start());


If this is not what you're looking for, I'd like more info on what you need to accomplish.

Thanks
answered by gilz (14.5k points)
0 votes
Thank you for the quick reply and I want to start out by saying sorry for such the vague explanation on what I am trying to accomplish.

I have an html report that gets generated when a menu item is clicked. Part of the generation method we prompt the user asking them if they would like to view the file via a message box.

if (reportSave  != null)
{
string msg ="View?"   
DialogResult result = MessageBox.Show (null,
         msg,
         "View Report?",
         MessageBoxButtons.YesNoCancel,
         MessageBoxIcon.Question,
         MessageBoxDefaultButton.Button1);
      
if(DialogResult.Yes == result)
   {
         // Load HTML file.
System.Diagnostics.Process p = new System.Diagnostics.Process();
p = System.Diagnostics.Process.Start(reportSave.FileName);
   }
}


I'm trying to mock out the Process p. Looking up the start method in the context of the reportSave.FileName, Start returns a System.Diagonstics Process.

Here is a snippet from the object browser inside visual studio

public static System.Diagnostics.Process Start ( System.Diagnostics.ProcessStartInfo startInfo )
Member of System.Diagnostics.Process


I hope this explanation helps, if you would like more information please let me know.

Thank you

Wesley
answered by Deadeye (220 points)
0 votes
Hi Wesly,

This makes much more sense, thank you for this explanation.

I've rechecked the issue and actually I did find a minor bug in there that might explain the difficulties.

However, I still feel that there is something wrong with the logic of the test.
Here is how i would write the test:
Process p = new Process();
Mock<Process> myMock = MockManager.Mock<Process>();
myMock.CallStatic.ExpectAndReturn("Start", p);


As you can see Ive moved the creation of the returned process before the declaration of the mock. otherwise the isolator will actual returned from the call to Start would be a "mocked" instance.
In case this is exactly what you wanted i would go for this:
MockObject<Process> myMock = MockManager.MockObject<Process>();
myMock.CallStatic.ExpectAndReturn("Start", myMock.Object);

At any case you can either of the examples. Both should work for you.


:?: what is the purpose of this line (in the real code)?
System.Diagnostics.Process p = new System.Diagnostics.Process();

i think it is not needed there. you can replace that part (both lines) with:
Process p = Process.Start(reportSave.FileName); 
answered by lior (13.2k points)
0 votes
it worked :mrgreen:

I used:
MockObject myMock = MockManager.MockObject(typeof(Process));
myMock.CallStatic.ExpectAndReturn("Start", myMock.Object);


I tried
MockObject<Process> myMock = MockManager.MockObject<Process>();
myMock.CallStatic.ExpectAndReturn("Start", myMock.Object);

but kept getting compile errors of "Exspect ')'" on this part of the code marked in red

MockObject<Process>();

I'm not sure why this is.

I'm just learning this code and I've found a few instances of the full path of the object even though there is no need for it. The purpose of the code is to launch IE to view the generated html report.

Thank you for the quick response

Wesley
answered by Deadeye (220 points)
...