.NET NUnit 测试 - Assembly.GetEntryAssembly() 为 null

发布于 2024-10-05 15:14:22 字数 195 浏览 3 评论 0 原文

当使用 Assembly.GetEntryAssembly() 的类在单元测试中运行时,Assembly.GetEntryAssembly()null

在单元测试期间是否有一些选项如何定义 Assembly.GetEntryAssembly()

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九局 2024-10-12 15:14:22

中给出的 SetEntryAssembly(Assembly assembly) 方法

实现http:// /frostwave.googlecode.com/svn-history/r75/trunk/F2DUnitTests/Code/AssemblyUtilities.cs

到您的单元测试项目。

         /// <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);
        }

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);
        }
离线来电— 2024-10-12 15:14:22

您可以使用 Rhino Mocks 执行类似的操作:将 Assembly.GetEntryAssembly() 调用封装到具有接口 IAssemblyLoader 的类中,并将其注入到您正在测试的课程。这尚未经过测试,但大致如下:

[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();
  }
}

更新:Rhino Mocks 不再积极维护,但可以与其他模拟库(例如 起订量

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文