Question

In order to protect against poor designer merges that mess up control initialization, I decided to add some "unit tests" that try to create instances of my System.Windows.Forms.Form objects, like so:

[TestMethod]
public void TestMyForm()
{
    var form = new MyForm();//if exception is thrown, fail test...
}

Unfortunately, DevExpress doesn't seem to like having its controls instantiated from within the unit test runner and it shows a nagging splash screen with "License expired" that requires user interaction in order to continue running the tests. How can this be avoided?

Was it helpful?

Solution

After some hours of thorough digging, I discovered that System.ComponentModel.Design.RuntimeLicenseContext.GetSavedLicenseKey() is being called by DevExpress and this method makes a call to Assembly.GetEntryAssembly(), which happens to return null when called from within a unit test, and this makes DevExpress believe that its being used in designer mode, which indeed requires a valid license.

Having this clue, I managed to find a really underrated answer from user @Manjay that provides an elegant solution to this problem by using reflection. I took the liberty to provide the following slightly modified version of Jamie Cansdale's code, which can be found here:

public static void SetEntryAssembly()
{
    if (Assembly.GetEntryAssembly() != null)
    {
        return;
    }

    Assembly assembly = Assembly.GetCallingAssembly();

    AppDomainManager manager = new AppDomainManager();
    FieldInfo entryAssemblyfield = manager.GetType().GetField("m_entryAssembly", BindingFlags.Instance | BindingFlags.NonPublic);
    if (entryAssemblyfield != null)
    {
        entryAssemblyfield.SetValue(manager, assembly);
    }

    AppDomain domain = AppDomain.CurrentDomain;
    FieldInfo domainManagerField = domain.GetType().GetField("_domainManager", BindingFlags.Instance | BindingFlags.NonPublic);
    if (domainManagerField != null)
    {
        domainManagerField.SetValue(domain, manager);
    }
}

OTHER TIPS

You may want to check out Coded UI Tests for testing the UI of WinForms applications. http://msdn.microsoft.com/en-us/library/dd286726.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top