c# - unit test dynamic loading code -
I was reading and found this code as an aswer as a question
Public listing & lt; T & gt; LoadPlugin & lt; T & gt; (String directory) {type interface type type = type (t); & Lt; T & gt; Implementation = new list & lt; T & gt; (); // TODO: Check to ensure the type, valid foreach (var file in System.IO.Directory.GetFiles (directory)) {// TODO: Include the proper file handle here and / ensure that the files are not. Try Vari type in Dll {foreach (system.Reflection.Assembly.LoadFile (file). GetTypes ()) {if (interfaceType.IsAssignableFrom (type) & InterfaceType! = Type) // // Applicable class Found: / / TODO: Check additional to ensure interface / pre-specified any // requirement: make sure that the type is a class, default constructor etc. Example = (t) activator. CreateInstance (type); Implementations.Add (example); }}} Hold {}} return deployment; }
And it was thinking that would be the best way to test this code?
There are three unrelated things that are done in a method (see). Each of them should be separated into their class, which implements some interfaces so that you can duplicate them for better test eligibility. Tree things are:
-
Finding .dll files from the plugin directory.
-
Loading the .dll and getting the types included in it should be a one-liner that says API methods. You do not really need to do this test (at least in unit tests), because you can reasonably believe that programming language libraries do the right thing.
-
Creating Examples of Plugins
When the algorithm is divided into three parts, then you can do unit test parts in 1 and 3 individually (although technically the test unit for Part 1 There are no tests, because this file touches the system, unless C # has a duplicate of the file system, such as Java 7's NIO2 file system API must be fake). You can test the same unit that connects them all together, by joking the part 2.
Comments
Post a Comment