7

I am using C#. I have created a Wix installer and a custom action to support the wix installer. Now I am trying to create a unit test the CustomAction only, without LUX.

I have tried in many different ways, but I am unable to Mock the Microsoft.Deployment.WindowsInstaller Session. Any idea or pointers. I am using Moq.

user2824187
  • 81
  • 1
  • 4
  • do you know for a fact if this is even possible without LUX? or are you asking if it is possible? – jazb Oct 17 '18 at 03:31
  • Hi, Sorry for digging out an old topic but I have the same question. Did you finally find a solution to your problem ? Thanks – Morgane Jul 20 '21 at 11:17
  • @Morgane: I added an obvious answer. Let me know if you found something better. – Nick Westgate Aug 18 '21 at 12:12

1 Answers1

2

It's not pretty, but I created a simple wrapper for the session. Something like:

public class MockSession
{
    private readonly Session _session = null;
    private readonly Dictionary<string, string> _properties;

    public MockSession()
    {
        _properties = new Dictionary<string, string>();
    }

    public MockSession(Session session)
    {
        _session = session;
    }

    public string this[string property]
    {
        get
        {
            if (_session)
                return _session[property];
            else
                return _properties[property];
        }
        set
        {
            if (_session)
                _session[property] = value;
            else
                _properties[property] = value;
        }
    }
}

Each CustomAction method is a stub which wraps the session:

[CustomAction]
public static ActionResult Method(Session session)
{
    var mockSession = new MockSession(session);
    return MethodMock(mockSession);
}

public static ActionResult MethodMock(MockSession session)
{
    // ... The real work here is testable
}

Not ideal, but it works in a pinch.

Nick Westgate
  • 3,088
  • 2
  • 34
  • 41