This answer demonstrates how to determine which tests execute a Dummy method but does not answer:
inject different implementation somewhere deep inside the code to modify during runtime the behavior of some class
Reflection doesn't provide granular access to the IL Body of the unit test methods which you will need; however Cecil
provides this functionality. The following linq returns a list of methods that internally call DoDummyThings1
. The linq could be more efficient but I wanted to make it as clear as possible. The where
clause is the important part.
//syntax based on version 0.9.5.4 (http://nuget.org/packages/Mono.Cecil/0.9.5.4)
using Mono.Cecil;
using Mono.Cecil.Cil;
//...
string assemblyPath = (@"path to your unit test assembly\MyTests.dll");
AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(assemblyPath);
List<MethodDefinition> testsThatCallDummyMethods =
(from mod in asm.Modules
from t in mod.Types
from meth in t.Methods
where meth.HasBody
from instr in meth.Body.Instructions
let op = instr.Operand as MethodDefinition
where
instr.OpCode == OpCodes.Callvirt &&
op != null &&
op.DeclaringType.FullName ==
"Lib.Dummy" //namespace qualified type name
&& op.Name ==
"DoDummyThings1" //method names...
select meth)
.ToList();
Disassemble the test assembly using ILDasm
to figure out the OpCodes / Operands. The relevant part of the TestDummy
method will be something like:
//this.dum.DoDummyThings1();
IL_0001: ldarg.0
IL_0002: ldfld class Lib.Dummy Lib.UnitTest::dum
IL_0007: callvirt instance void Lib.Dummy::DoDummyThings1()