.NET NUnit测试

编程入门 行业动态 更新时间:2024-10-14 06:17:21
本文介绍了.NET NUnit测试 - Assembly.GetEntryAssembly()为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在使用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?

推荐答案

您可以做这样的事情与犀牛嘲笑:封装Assembly.GetEntryAssembly()调用到一个类的接口IAssemblyLoader,注入类的都是测试。这不是测试,但沿着这个路线的东西:

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

更多推荐

.NET NUnit测试

本文发布于:2023-11-14 20:10:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   NET   NUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!