English 中文(简体)
使用反射来创建在构建时未知的界面模拟
原标题:Using Reflection to create a Mock of an interface that is unknown during buildtime

对于我的内装剂 结束测试, 重要的是,我的测试组装 不要预先装上 影子的影印 附属组装。

Assembly T is the Testframework that loads and tests Assembly A. Assembly A depends on interfaces defined in B.

为测试目的,我必须替换A类中一些静态成员,但不能在施工期间使用。

以下是一些假代码, 说明我所处的困境:

        A_assembly = Assembly.LoadFrom("A.dll");
        A_type = A_assembly.GetType("TheSingleton.Master", true);
        MethodInfo Master_Init_Info = type.GetMethod("Init", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
        //before init is called some things need to be replaced
        FieldInfo semiknown = A_type.GetField("needsmocking", BindingFlags.Public | BindingFlags.Static));
        ??? mock_semiknown = MockRepository.GenerateMock<???>();
        semiknown.SetValue((???)mock_semiknown, mock_semiknown);
        //testing makes only sense if that static is replaced.
        Master_Init_Info.Invoke(null, null);
  1. I can access the type via semiknown.FieldType but what good does that do me? Can i use that information somehow to create the mock and replace the static member with it?
  2. Suppose i get the type and am able to replace the static member - how can i build my expectancies in the mock?
最佳回答

你可以用反光来称呼它

var method = typeof(MockRepository).GetMethod("GenerateMock").MakeGenericMethod(semiknown.FieldType);
var mock_semiknown = method.Invoke(null, null);
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签