嗨,我想知道是否有人知道我的问题的答案:
考虑以下代码
class TheHandler
{
...
Public EventHandler myRealWorldEvent;
...
}
class TheSubscriber
{
private TheHandler myHandler = new TheHandler();
public subscribeToHandler()
{
myHandler.myRealWorldEventHandler += OnSomethingHappens;
}
...
pirvate OnSomeThingHappens()
{
...
}
}
我的问题是 - >我如何测试(仅限NUnit)OnSomethingHappens订阅了myRealWorldEventHandler。我不能改变SUT /生产代码,我不能模拟(Moq / Nmock等)。有谁知道我的问题的解决方案?
致以最诚挚的问候,
zhengtonic
答案 0 :(得分:1)
NUnit不这样做 - 测试某些私有处理程序是否订阅了某些私有字段。涉及太多私人物品。但是,从反射中获得一些帮助,你无能为力。请注意,这不是漂亮的代码:
var subscriber = new TheSubscriber();
var handlerField = typeof(TheSubscriber)
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
// if field with such name is not present, let it fail test
.First(f => f.Name == "myHandler");
var handlerInstance = handlerField.GetValue(subscriber);
var someEventField = typeof(TheHandler)
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(f => f.Name == "myRealWorldEvent");
var eventInstance = (EventHandler) someEventField.GetValue(handlerInstance);
var subscribedMethod = eventInstance
.GetInvocationList()
.FirstOrDefault(d => d.Method.Name == "OnSomethingHappens");
Assert.That(subscribedMethod, Is.Not.Null);
如果你必须处理许多遗留系统测试(即私有成员,静态成员 - 免费框架无法正常处理的东西),我建议你看一下像{{}这样的工具。 3}}或TypeMock。
答案 1 :(得分:0)
有同样的问题。来自jimmy_keen的代码与旧.NET无法正常工作。通过编写辅助方法来解决它:
public static void assertSubscribed<EventHandlerType>(object handler, object subscriber, string eventName = null) {
var inappropriate = false;
try {
if (!typeof (EventHandlerType).IsSubclassOf(typeof (Delegate)) ||
typeof (EventHandlerType).GetMethod("Invoke").ReturnType != typeof (void))
inappropriate = true;
} catch (AmbiguousMatchException) {
inappropriate = true;
} finally {
if (inappropriate) throw new Exception("Inappropriate Delegate: " + typeof (EventHandlerType).Name);
}
var handlerField = subscriber.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(h => h.FieldType.IsInstanceOfType(handler));
var handlerInstance = handlerField == null ? null : handlerField.GetValue(subscriber);
var eventField = handlerInstance == null ? null : handlerInstance.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(f => (f.FieldType.IsAssignableFrom(typeof (EventHandlerType)) &&
(eventName == null || eventName.Equals(f.Name))));
var eventInstance = eventField == null ? null : (Delegate)eventField.GetValue(handlerInstance);
var subscribedMethod = eventInstance == null
? null
:eventInstance.GetInvocationList().FirstOrDefault(
d => d.Method.DeclaringType != null && d.Method.DeclaringType.IsInstanceOfType(subscriber));
Assert.That(subscribedMethod, Is.Not.Null);
}
&#34;不&#34;方法:
public static void assertNotSubscribed<EventHandlerType>(object handler, object subscriber, string eventName = null) {
var inappropriate = false;
try {
if (!typeof (EventHandlerType).IsSubclassOf(typeof (Delegate)) ||
typeof (EventHandlerType).GetMethod("Invoke").ReturnType != typeof (void))
inappropriate = true;
} catch (AmbiguousMatchException) {
inappropriate = true;
}
if (inappropriate) return;
var handlerField = subscriber.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(h => h.FieldType.IsInstanceOfType(handler));
var handlerInstance = handlerField == null ? null : handlerField.GetValue(subscriber);
var eventField = handlerInstance == null ? null : handlerInstance.GetType()
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.First(f => (f.FieldType.IsAssignableFrom(typeof (EventHandlerType)) &&
(eventName == null || eventName.Equals(f.Name))));
var eventInstance = eventField==null?null:(Delegate) eventField.GetValue(handlerInstance);
var subscribedMethod = eventInstance == null
? null
: eventInstance.GetInvocationList().FirstOrDefault(
d => d.Method.DeclaringType != null && d.Method.DeclaringType.IsInstanceOfType(subscriber));
Assert.That(subscribedMethod, Is.Null);
}
并且调用:
assertSubscribed<EventHandler>(handler, subscriber);
assertNotSubscribed<EventHandler>(handler, subscriber);
assertSubscribed<EventHandler>(handler, subscriber, "myRealWorldEvent");
assertNotSubscribed<EventHandler>(handler, subscriber, "myRealWorldEvent");
不要为代码风格惯例而烦恼,但这种方法看起来很紧凑。