Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to mock Microsoft.Deployment.WindowsInstaller Session

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.

like image 413
user2824187 Avatar asked Oct 17 '18 03:10

user2824187


Video Answer


1 Answers

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.

like image 76
Nick Westgate Avatar answered Sep 21 '22 13:09

Nick Westgate