Search code examples
c#unit-testingtypemock

How to make TypeMock Ignore All MessageBox


I have this annoying method which pop up a MessageBox. So when I try to test it I want to do something like this.

    /// <summary>
    /// A test for LoadConfig exception
    /// </summary>
    [TestMethod]
    public void LoadConfigTest1()
    {
        // Arrange
        var target = new RFIDManager();
        Isolate.WhenCalled(() => ConfigurationManager.AppSettings[0]).WillThrow(new Exception("foo"));
        Isolate.WhenCalled(() => MessageBox.Show()).IgnoreCall();

        // Act
        var result = target.LoadConfig();

        // Assert
        Assert.IsFalse(result);
    }

This don't compile. Because

MessageBox.Show()

needs a string argument. So I want to know is it possible to specify ignore call always, no matter what is the argument ?

I don't know the exact string that will show up.

Thanks a lot !


Solution

  • Try

    Isolate.WhenCalled(() => MessageBox.Show(null)).IgnoreCall();
    

    That should ignore all MessageBox.Show().