c# - Exception creating an App Domain in NUnit test -
i’ve written plugin library searches directory assemblies export implementations of given interface. loading assemblies temporary app domain in reflection context , searching through exported types. temporary app domain unloaded, assemblies of involvement loaded default app domain , objects of found types instantiated utilize through searched-for interface.
i'm trying write unit tests process using nunit, i'm getting filenotfoundexception in unit tests, when trying wire reflectiononlyassemblyresolve event. here's bare-bones example:
using system; using system.reflection; using nunit.framework; namespace nunitappdomains { [testfixture] public class testclass { [test] public void nunittest() { var advertisement = appdomain.createdomain("somename"); // next line throws filenotfoundexception, complaining not beingness // able find test assembly itself... o.o ad.reflectiononlyassemblyresolve += somehandler; appdomain.unload(ad); } static assembly somehandler(object sender, resolveeventargs args) { // code here throw new notimplementedexception(); } } }
i attempting test code on dummy assemblies in known location, of do/don't contain valid imlementation/s. code not amenable unit testing or if is, how can avoid these exceptions? thanks
the problem code running in different application base of operations (nunit's or visual studio's or whatever running test). when create domain without specifying base of operations uses base of operations of application running code, e.g. "program files\nunit\bin", , of course of study assembly can't found there.
the solution utilize application base of operations of code when creating appdomain
, can current thread:
var callingdomain = thread.getdomain(); var setup = new appdomainsetup { applicationbase = callingdomain.setupinformation.applicationbase }; var advertisement = appdomain.createdomain("somename", null, setup);
this blog post goes little more detail, not much. still worth read though.
c# unit-testing plugins nunit appdomain
No comments:
Post a Comment