Skip to content

Test using MessageBox by Typemock Isolator

Akira Sugiura edited this page Oct 12, 2015 · 11 revisions

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. Perform the operation while referring the following image. This is the operation for IsolatorMigrationDemo...

PS 11.IsolatorMigration> $TargetReferencedAssembly.GetTypes()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    SomeClass                                System.Object
True     False    UserOfSomeClass                          System.Object


PS 11.IsolatorMigration> $TargetReferencedAssembly.GetTypes() | ? { $_.Name -match 'Class' }

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    SomeClass                                System.Object
True     False    UserOfSomeClass                          System.Object


PS 11.IsolatorMigration> $TargetReferencedAssembly.GetTypes() | ? { $_.Name -match 'Class' } | pfind

Method
------
Void MyMethod()
Void .ctor()
Void DoSomething()
Void .ctor()


PS 11.IsolatorMigration> $TargetReferencedAssembly.GetTypes() | ? { $_.Name -match 'Class' } | pfind | pget | clip    #Paste to IsolatorMigration.v4.0.30319.v1.0.0.0.prig
PS 11.IsolatorMigration> exit

and this is the operation for System.Windows.Forms:

PS 11.IsolatorMigration> $TargetReferencedAssembly.GetTypes() | ? { $_.Name -eq 'messagebox' }

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    MessageBox                               System.Object


PS 11.IsolatorMigration> $TargetReferencedAssembly.GetTypes() | ? { $_.Name -eq 'messagebox' } | pfind -m 'show\(
system\.string\)'

Method
------
System.Windows.Forms.DialogResult Show(System.String)


PS 11.IsolatorMigration> $TargetReferencedAssembly.GetTypes() | ? { $_.Name -eq 'messagebox' } | pfind -m 'show\(system\.string\)' | pget | clip    # Paste to System.Windows.Forms.v4.0.30319.v4.0.0.0.prig
PS 11.IsolatorMigration> exit

Go back to Visual Studio, paste each indirection settings to IsolatorMigration.v4.0.30319.v1.0.0.0.prig and System.Windows.Forms.v4.0.30319.v4.0.0.0.prig. If 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(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(in this case, PSomeClass.MyMethod().Body). I described earlier about Isolate.WhenCalled(..).WillReturn and Isolate.Verify.WasCalledWithExactArguments, so... oh, 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 😈