Unit Test to verify that WinForms application doesn't load Assembly more than once

旧街凉风 提交于 2019-12-11 19:35:36

问题


I am trying to write a unit test (NUnit) that will:

  1. Create an instance of some Form.
  2. Hook up the relevant AssemblyLoad event of the AppDomain to build a List of loaded assembly names.
  3. If the same assembly is loaded twice, fail.
  4. Otherwise - pass.

I cannot seem to get the logic for this... The test always passes.

Can this be done ?


回答1:


It is hard to make your unit test fail. The CLR already makes sure that an assembly only gets loaded once. Pretty important, getting the same assembly loaded more than once produces very hard to diagnose casting errors at runtime.

You'd have to use the horrid Assembly.LoadFile() to trip a fail. Avoid testing things you should never do to begin with.




回答2:


Once you load an assembly in the AppDomain, you cannot load it again and there doesn't appear to be an Assembly.Unload method either. Well, technically you can unload the assembly if you unload all of the AppDomains that loaded it.



来源:https://stackoverflow.com/questions/6458297/unit-test-to-verify-that-winforms-application-doesnt-load-assembly-more-than-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!