我有一個服務負責訂閱EWS以獲取新的郵件通知。我爲服務創建了一個接口來模擬它並測試一個虛擬實現。然而,每當我嘗試手動告訴我的事件應該做什麼時,我都會碰壁。如何使用內部構造函數模擬事件
這是我的具體實現。
public interface IExchangeService
{
void Subscribe();
}
public class ExchangeServiceSubscriber : IExchangeService
{
private readonly ExchangeService _exchangeService;
private readonly IConsumer<IEmail> _consumer;
public ExchangeServiceSubscriber(
ExchangeService exchangeService,
IConsumer<IEmail> consumer)
{
_exchangeService = exchangeService;
_consumer = consumer;
}
public void Subscribe()
{
// code to subscribe
streamingConnection.OnNotificationEvent += OnEvent;
streamingConnection.Open();
}
public void OnEvent(object sender, NotificationEventArgs args)
{
foreach (NotificationEvent triggeredEvent in args.Events)
{
if (triggeredEvent is ItemEvent)
{
var propertySet = new PropertySet(ItemSchema.UniqueBody, ItemSchema.Attachments)
{
RequestedBodyType = BodyType.Text
};
EmailMessage email = EmailMessage.Bind(args.Subscription.Service,
((ItemEvent)triggeredEvent).ItemId, propertySet);
_consumer.Consume(new ExchangeEmail { Body = email.UniqueBody });
}
}
}
}
而且不幸的是,幾乎在每一個EWS類要麼密封或具有真正限制我如何去耦,似乎內部構造。我試圖設置NotificationEventArgs
(例如)的期望值,但它使用了一個內部構造函數。
這是我一直在擺弄的一些想法。你可以閱讀關於嘲笑事件here。
mock.Setup(x => x.OnEvent(new object(), new NotificationEventArgs()));
問題是NotificationEventArgs
使用內部構造函數。
我可以看到使用某種封裝工作,但我不完全確定它會是什麼樣子。其中一個重大問題是EWS幾乎無法防止任何人手動注入依賴關係。我基本上試圖測試,無論何時事件OnEvent
引發電子郵件實際上將被消耗。另外,雖然我想測試這個功能,但我不確定值得與EWS的每一步戰鬥。
你什麼時候寫的'inline'你的意思'internal'?代碼示例中使用'SubscriptionErrorEventArgs'的位置在哪裏? – 2011-04-05 16:39:47
是的,你是對的。我的意思是「內部」。我的意思是寫'NotificationEventArgs'而不是'SubscriptionErrorEventArgs'。請參閱編輯。 – Mike 2011-04-05 16:42:26
您能否請您展示您當前不使用的方法?原因:我不知道最小起訂量,但是你的方法仍然可以讓我想出一些想法。 – 2011-04-05 16:46:01