0

I need to avoid(mock) unit testing of line of code displaying messagebox using FakeItEasy framework.

Code is here,

public class XYZ
{
    public static int nValue = 0;

    public void AddInetegers(int i)
    {

        int j = i * 100;
        int k = j * 30 / 100;

        MessageBox.Show("Value Here {0}",  k.ToString());

        nValue = k;
    }
}
Sujeet Singh
  • 165
  • 3
  • 13

1 Answers1

2

MessageBox.Show is a static member, and cannot be faked by FakeItEasy. In these situations, typically the code that accesses the non-fakeable service is isolated and wrapped in an interface.

Here's how I would do it:

interface IAlerter
{
    void Alert(string text, string caption);
}

public class XYZ
{
    public static int nValue = 0;

    private readonly IAlerter _alerter;
    public XYZ(IAlerter alerter)
    {
        _alerter = alerter;
    }

    public void AddInetegers(int i)
    {

        int j = i * 100;
        int k = j * 30 / 100;

        _alerter.Show("Value Here {0}",  k.ToString());

        nValue = k;
    }
}

[TestMethod]
public void Test_Using_FakeItEasy()
{
    var alerter = A.Fake<IAlerter>();
    // No need to configure alerter.Alert(), since it doesn't return a value

    var xyz = new XYZ(alerter);
    xyz.AddInetegers(3);
    Assert.AreEqual(90, XYZ.nValue);
}
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758