本文介绍了.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测试 - Assembly.GetEntryAssembly()为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!