lysergic-acid
lysergic-acid

Reputation: 20050

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

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 ?

Upvotes: 0

Views: 537

Answers (2)

Hans Passant
Hans Passant

Reputation: 941337

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.

Upvotes: 8

Kiril
Kiril

Reputation: 40345

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.

Upvotes: 1

Related Questions