Hi Michael
Is it possible (and if how) to mock static classes and their static methods?
:( If you are talking about .NET 2.0 static classes then TypeMock 2.3 doesn't support .NET 2.0, (A Beta program for .NET 2.0 will be starting soon)
:arrow: Mocking static methods is simple and is one of TypeMocks beauties, just mock the class and the method, see the "first mock" topic in the documentation and the examples included in the installation folder (if you installed them).
:idea: There is no difference if you use Mock or MockAll for static members as static members have no instance.
I don't get the type for "Clipboard" in normal matter (that is Type.GetType("System.Windows.Forms.Clipboard")).
:?: How do you get the type? Why not use typeof(Clipboard)?
is TypeMock able to mock an already instantiated (static) class.
:arrow: I don't quite follow, what is an instantiation of a static class?
Anyhow, TypeMock can mock already instantiated classes simply by using MockAll
(Note: Types that are instantiated BEFORE MockManager.Init() is called for the first time cannot be mocked, See "Fast Mode" in documentation for more details.)
Here is an example of your mock- all other methods run as normal (Constructor.NotMocked)
Mock clipboardMock = MockManager.MockAll(typeof(Clipboard),Constructor.NotMocked);
clipboardMock.ExpectAndReturn("GetDataObject",myIDataObject);
clipboardMock.ExpectCall("SetDataObject");
:idea: If you are using the enterprise edition you can also swap the parameters of the mocked methods
clipboardMock.ExpectUnmockedCall("SetDataObject").When(new Assign("test text").AndCheck("real text"));
The above code will call SetDataObject but will pass "test text" when "real text" is passed!!!
Of course if your tested code just uses GetDataObject, you can Clipboard.SetDataObject("test text") before calling you code, and you won't need to mock any classes