When class used Assembly.GetEntryAssembly() run in unit test, the Assembly.GetEntryAssembly() is null.
Is there some option how define Assembly.GetEntryAssembly() during unit testing?
When class used Assembly.GetEntryAssembly() run in unit test, the Assembly.GetEntryAssembly() is null.
Is there some option how define Assembly.GetEntryAssembly() during unit testing?
Implement the SetEntryAssembly(Assembly assembly) method given in
http://frostwave.googlecode.com/svn-history/r75/trunk/F2DUnitTests/Code/AssemblyUtilities.cs
to your unit test project.
/// <summary>
/// Use as first line in ad hoc tests (needed by XNA specifically)
/// </summary>
public static void SetEntryAssembly()
{
SetEntryAssembly(Assembly.GetCallingAssembly());
}
/// <summary>
/// Allows setting the Entry Assembly when needed.
/// Use AssemblyUtilities.SetEntryAssembly() as first line in XNA ad hoc tests
/// </summary>
/// <param name="assembly">Assembly to set as entry assembly</param>
public static void SetEntryAssembly(Assembly assembly)
{
AppDomainManager manager = new AppDomainManager();
FieldInfo entryAssemblyfield = manager.GetType().GetField("m_entryAssembly", BindingFlags.Instance | BindingFlags.NonPublic);
entryAssemblyfield.SetValue(manager, assembly);
AppDomain domain = AppDomain.CurrentDomain;
FieldInfo domainManagerField = domain.GetType().GetField("_domainManager", BindingFlags.Instance | BindingFlags.NonPublic);
domainManagerField.SetValue(domain, manager);
}
You could do something like this with Rhino Mocks: Encapsulate the Assembly.GetEntryAssembly() call into a class with interface IAssemblyLoader and inject it into the class your are testing. This is not tested but something along the lines of this:
[Test] public void TestSomething() {
// arrange
var stubbedAssemblyLoader = MockRepository.GenerateStub<IAssemblyLoader>();
stubbedAssemblyLoader.Stub(x => x.GetEntryAssembly()).Return(Assembly.LoadFrom("assemblyFile"));
// act
var myClassUnderTest = new MyClassUnderTest(stubbedAssemblyLoader);
var result = myClassUnderTest.MethodToTest();
// assert
Assert.AreEqual("expected result", result);
}
public interface IAssemblyLoader {
Assembly GetEntryAssembly();
}
public class AssemblyLoader : IAssemblyLoader {
public Assembly GetEntryAssembly() {
return Assembly.GetEntryAssembly();
}
}
Update: Rhino Mocks is no longer actively maintained but could do something similar with other mocking libraries such as Moq