-
Notifications
You must be signed in to change notification settings - Fork 21
Test using MessageBox by Typemock Isolator
Typemock introduces the sample that replaces MessageBox to a mock in Isolator's Quick Start. Also, you can migrate it to Prig(with Moq).
First, you have to create the indirection settings. Open the Package Manager Console and change Default project:
to your test project. Then, execute the following commands:
PM> dir
Directory: C:\users\akira\documents\visual studio 2013\Projects\IsolatorMigrationDemo
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2014/10/10 9:31 IsolatorMigrationDemo
d---- 2014/10/10 9:37 IsolatorMigrationDemoTest
d---- 2014/10/10 9:37 packages
-a--- 2014/10/10 9:32 1561 IsolatorMigrationDemo.sln
PM> cd .\IsolatorMigrationDemo\bin\Debug
PM> dir
Directory: C:\users\akira\documents\visual studio 2013\Projects\IsolatorMigrationDemo\IsolatorMigrationDemo\bin\Debug
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014/10/10 9:31 7680 IsolatorMigrationDemo.exe
-a--- 2014/10/10 9:30 189 IsolatorMigrationDemo.exe.config
-a--- 2014/10/10 9:31 28160 IsolatorMigrationDemo.pdb
-a--- 2014/10/10 9:30 23168 IsolatorMigrationDemo.vshost.exe
-a--- 2014/10/10 9:30 189 IsolatorMigrationDemo.vshost.exe.config
-a--- 2013/06/18 21:28 490 IsolatorMigrationDemo.vshost.exe.manifest
PM> padd -af (dir .\IsolatorMigrationDemo.exe).FullName
PM> padd -as "System.Windows.Forms, Version=4.0.0.0"
PM>
Next, get the indirection settings for the method that is used in the sample of Isolator. Open PowerShell, and execute the following commands to get the information:
PS> $pwd
Path
----
C:\Users\Akira\Documents\Visual Studio 2013\Projects\IsolatorMigrationDemo\IsolatorMigrationDemo\bin\Debug
PS> powershell
Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.
PS> ipmo "C:\Users\Akira\Documents\Visual Studio 2013\Projects\IsolatorMigrationDemo\packages\Prig.0.0.0-alpha9\tools\Urasandesu.Prig"
PS> dir
Directory: C:\Users\Akira\Documents\Visual Studio 2013\Projects\IsolatorMigrationDemo\IsolatorMigrationDemo\bin\Debug
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014/10/10 9:31 7680 IsolatorMigrationDemo.exe
-a--- 2014/10/10 9:30 189 IsolatorMigrationDemo.exe.config
-a--- 2014/10/10 9:31 28160 IsolatorMigrationDemo.pdb
-a--- 2014/10/10 9:30 23168 IsolatorMigrationDemo.vshost.exe
-a--- 2014/10/10 9:30 189 IsolatorMigrationDemo.vshost.exe.config
-a--- 2013/06/18 21:28 490 IsolatorMigrationDemo.vshost.exe.manifest
PS> $asmInfo = [System.Reflection.Assembly]::LoadFrom((dir .\IsolatorMigrationDemo.exe).FullName)
PS> $asmInfo.GetTypes()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False Form1 System.Windows.Forms.Form
False False Program System.Object
True False SomeClass System.Object
True False UserOfSomeClass System.Object
False False Resources System.Object
False False Settings System.Configuration.ApplicationSettingsBase
PS> $asmInfo.GetTypes() | ? { $_.Name -match 'Class' }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False SomeClass System.Object
True False UserOfSomeClass System.Object
PS> $asmInfo.GetTypes() | ? { $_.Name -match 'Class' } | pfind
Method
------
Void MyMethod()
Void .ctor()
Void DoSomething()
Void .ctor()
PS> $asmInfo.GetTypes() | ? { $_.Name -match 'Class' } | pfind | pget | clip # About this results, paste to IsolatorMigrationDemo.v4.0.30319.v1.0.0.0.prig
PS> $asmInfo = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
PS> $asmInfo.GetTypes() | ? { $_.Name -eq 'messagebox' }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False MessageBox System.Object
PS> $asmInfo.GetTypes() | ? { $_.Name -eq 'messagebox' } | pfind -m 'show\(system\.string\)'
Method
------
System.Windows.Forms.DialogResult Show(System.String)
PS> $asmInfo.GetTypes() | ? { $_.Name -eq 'messagebox' } | pfind -m 'show\(system\.string\)' | pget | clip # About this results, paste to System.Windows.Forms.v4.0.30319.v4.0.0.0.prig
PS> exit
PS>
Go back to Visual Studio, paste each indirection settings to IsolatorMigrationDemo.v4.0.30319.v1.0.0.0.prig
and System.Windows.Forms.v4.0.30319.v4.0.0.0.prig
. After the build is succeeded, let's migrate the examples!
###Example Test 1 - Simple test using MessageBox Isolator also has powerful feature that replaces any methods by Profiling API. Moreover, as same as JustMock, it has the feature to generate Mock Object. Prig doesn't support it, but it is achieved by combination with Moq.
[Test]
public void MessageBoxShow_should_be_callable_indirectly()
{
using (new IndirectionsContext())
{
// Arrange
var mockMessageBox = new Mock<IndirectionFunc<string, DialogResult>>();
mockMessageBox.Setup(_ => _(string.Empty)).Returns(DialogResult.OK);
PMessageBox.ShowString().Body = mockMessageBox.Object;
// Act
MessageBox.Show("This is a message");
// Assert
mockMessageBox.Verify(_ => _("This is a message"));
}
}
Isolate.WhenCalled
can be replaced by assigning the Mock Object that is performed setup by Moq.Mock<T>.Setup
to the indirection stub of Prig(in this case, PMessageBox.ShowString().Body
). Isolate.Verify.WasCalledWithExactArguments
is same function as Moq.Mock<T>.Verify
. There is no problem. Let's go next!
###Example Test 2 - Complex Test The example is called "Complex" but it is not very difficult :)
[Test]
public void UserOfSomeClassDoSomething_should_show_MessageBox_if_an_exception_is_thrown()
{
using (new IndirectionsContext())
{
// Arrange
PSomeClass.MyMethod().Body = () => { throw new Exception("foo"); };
var mockMessageBox = new Mock<IndirectionFunc<string, DialogResult>>();
mockMessageBox.Setup(_ => _(string.Empty)).Returns(DialogResult.OK);
PMessageBox.ShowString().Body = mockMessageBox.Object;
// Act
var user = new UserOfSomeClass();
user.DoSomething();
// Assert
mockMessageBox.Verify(_ => _("Exception caught: foo"));
}
}
If there are no special conditions, Isolate.WhenCalled(..).WillThrow
can be replaced by assigning the function that is set as throwing an exception directly to the indirection stub of Prig(in this case, PSomeClass.MyMethod().Body
). I described earlier about Isolate.WhenCalled(..).WillReturn
and Isolate.Verify.WasCalledWithExactArguments
, so... Uh, this is everything!
For your information, if the target is complex enough to write the test code though it has any MessageBox processing, I personally think there's a high possibility the design will be failure. It will be allowed against existing codes and legacy codes, but I hope that you don't use the power of darkness like this library against new product codes :)
-
Home
- QUICK TOUR [SRC]
- FEATURES
- CHEAT SHEET
- PACKAGE MANAGER CONSOLE POWERSHELL REFERENCE
- COMMAND LINE REFERENCE
- APPVEYOR SUPPORT
- MIGRATION
- From Microsoft Research Moles [SRC]
- From Microsoft Fakes [SRC]
- From Telerik JustMock [SRC]
- From Typemock Isolator [SRC]