Fixed issue with missing entry assembly in unit tests.

This commit is contained in:
Damian Büchel 2020-05-19 15:17:12 +02:00
parent 60731e01a8
commit 87882c3aa5

View file

@ -54,6 +54,8 @@ namespace SafeExamBrowser.Configuration.UnitTests
fileSaver.Setup(f => f.CanSave(It.IsAny<Uri>())).Returns<Uri>(u => u.IsFile);
networkLoader.Setup(n => n.CanLoad(It.IsAny<Uri>())).Returns<Uri>(u => u.Scheme.Equals("http") || u.Scheme.Equals("seb"));
SetEntryAssembly();
sut = new ConfigurationRepository(certificateStore.Object, hashAlgorithm.Object, logger.Object);
sut.InitializeAppConfig();
}
@ -327,5 +329,22 @@ namespace SafeExamBrowser.Configuration.UnitTests
sut.Register(xmlParser.Object);
sut.Register(xmlSerializer.Object);
}
/// <summary>
/// Hack required for unit tests to be able to retrieve the <see cref="Assembly.GetEntryAssembly"/> while executing.
/// </summary>
public void SetEntryAssembly()
{
var assembly = Assembly.GetCallingAssembly();
var manager = new AppDomainManager();
var entryAssemblyfield = manager.GetType().GetField("m_entryAssembly", BindingFlags.Instance | BindingFlags.NonPublic);
entryAssemblyfield.SetValue(manager, assembly);
var domain = AppDomain.CurrentDomain;
var domainManagerField = domain.GetType().GetField("_domainManager", BindingFlags.Instance | BindingFlags.NonPublic);
domainManagerField.SetValue(domain, manager);
}
}
}