.NET NUnit test - Assembly.GetEntryAssembly() is null

前端 未结 2 2039
时光说笑
时光说笑 2020-12-01 10:31

When class used Assembly.GetEntryAssembly() run in unit test, the Assembly.GetEntryAssembly() is null.

Is there some option h

2条回答
  •  暖寄归人
    2020-12-01 11:03

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

提交回复
热议问题