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
MockManager.Init();
         Mock mock=MockManager.Mock(typeof(MessageBox));
            mock.ExpectAndReturn("Show",DialogResult.OK).Args("");
            MessageBox.Show("");
            mock.Verify();


using TestDriven.Net addin

if u run the above code through the Run Test(s) menu it pass ok but if u use Test With.../Debuger menu the messageBox window is not mocked.
Am i doing something wrong ?
asked by tolisss (28.8k points)

7 Answers

0 votes
Hi,
I don't know how this can work from the Nunit Add-in. Are you sure that the tests are actually being run (You can tell by seeing the 'passed' line). I have seen the Add-In not actually running the tests.
The MessageBox is one of the reserved types, we are currently working on reslving these issues with types from the System namespace.
Just as a note, TypeMock cannot mock pre-compiled types.
answered by scott (32k points)
0 votes
Hi
yes my tests runs ok i can set breakpoints and stop the debugger but when it finds MessageBox.Shpw is showing the window :(

what do u mean by
Just as a note, TypeMock cannot mock pre-compiled types.
answered by tolisss (28.8k points)
0 votes
Hi,

Currently TypeMock.NET cannot mock types from precompiled assemblies. :cry:
These assemblies will be supported in a future version :D

A precompiled assemby is actually native code. To precompile an assembly use the NGEN command.
Using precompiled assemblies will make the code run faster as there is no need to compile MSIL to native code.

The MessageBox.Show is precompiled and that is why it cannot be mock

Here is a list of microsofts precompiled assemblies. (found by running ngen /show)
:arrow: CustomMarshalers
:arrow: Microsoft.VisualStudio
:arrow: Microsoft.VSDesigner
:arrow: Microsoft.VSDesigner.Mobile
:arrow: mscorlib
:arrow: System
:arrow: System.Design
:arrow: System.Drawing
:arrow: System.Drawing.Design
:arrow: System.Windows.Forms
:arrow: System.Xml
:arrow: vjscor
:arrow: VJSharpCodeProvider
:arrow: vjslib
:arrow: vjslibcw
:arrow: VJSWfcBrowserStubLib
answered by scott (32k points)
0 votes
Hi,
In version 2.2 it is possible to mock MessageBox :D
answered by richard (3.9k points)
0 votes
My code is pretty much identical to the code in the first post of this thread. The Trace shows that System.Windows.Forms.MessageBox is being mocked, but the Show method is Static and not called. Richard, can you give me an example of code that sucessfully mocks MessageBox.Show()?

Thanks!
answered by lghummel (1.2k points)
0 votes
Hi,
This is an old post, thanks for searching before posting :-)

Here is an example that works under 3.1.3
Mock mock=MockManager.Mock(typeof(MessageBox));
mock.ExpectAndReturn("Show",DialogResult.OK);


if this doesn't help please send a sample of the failing code.
answered by richard (3.9k points)
0 votes
Stupid problem, I should setup the mock before executing the code that calls the MessageBox. Thanks for you help.
:oops:
answered by lghummel (1.2k points)
...