如何使用 NSubstitute 模拟 Outlook.MailItem 与多个附件?
How do I mock Outlook.MailItem with multiple Attachments using NSubstitute?
我正在尝试对接受 Outlook.MailItem 参数类型的静态函数进行单元测试:
public static bool GetMailAndDoSomething(Outlook.MailItem mailitem)
这是我目前的测试函数:
using Outlook = Microsoft.Office.Interop.Outlook;
...
public void TestGetMailAndDoSomething()
{
Outlook.MailItem mailItem = Substitute.For<Outlook.MailItem>();
Outlook.Attachments attachments = Substitute.For<Outlook.Attachments>();
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
mailItem.Attachments.GetEnumerator().Returns(attachments.GetEnumerator());
Assert.True(Misc.GetMailAndDoSomething(mailItem));
}
在GetMailAndDoSomething函数里面,有一段代码我要测试
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
//do someting
}
但是,测试程序永远不会到达 //do something
,因为 mailItem.Attachments 似乎总是空的。这是我在 IDE 在 foreach
行中断时看到的。
我需要在测试函数中修复什么,以便测试程序在目标函数中达到 //do something
?
您正在尝试在接口上执行实现。
Attachments
派生自 IEnumerable
[System.Runtime.InteropServices.Guid("0006303C-0000-0000-C000-000000000046")]
public interface Attachments : System.Collections.IEnumerable
考虑使用实际列表作为内存存储,然后将模拟设置为 return
var list = new List<Outlook.Attachment>();
list.Add(Substitute.For<Outlook.Attachment>());
list.Add(Substitute.For<Outlook.Attachment>());
list.Add(Substitute.For<Outlook.Attachment>());
list.Add(Substitute.For<Outlook.Attachment>());
Outlook.Attachments attachments = Substitute.For<Outlook.Attachments>();
attachments.GetEnumerator().Returns(list.GetEnumerator());
Outlook.MailItem mailItem = Substitute.For<Outlook.MailItem>();
mailItem.Attachments.Returns(attachments);
//...
我正在尝试对接受 Outlook.MailItem 参数类型的静态函数进行单元测试:
public static bool GetMailAndDoSomething(Outlook.MailItem mailitem)
这是我目前的测试函数:
using Outlook = Microsoft.Office.Interop.Outlook;
...
public void TestGetMailAndDoSomething()
{
Outlook.MailItem mailItem = Substitute.For<Outlook.MailItem>();
Outlook.Attachments attachments = Substitute.For<Outlook.Attachments>();
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
attachments.Add(Substitute.For<Outlook.Attachment>());
mailItem.Attachments.GetEnumerator().Returns(attachments.GetEnumerator());
Assert.True(Misc.GetMailAndDoSomething(mailItem));
}
在GetMailAndDoSomething函数里面,有一段代码我要测试
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
//do someting
}
但是,测试程序永远不会到达 //do something
,因为 mailItem.Attachments 似乎总是空的。这是我在 IDE 在 foreach
行中断时看到的。
我需要在测试函数中修复什么,以便测试程序在目标函数中达到 //do something
?
您正在尝试在接口上执行实现。
Attachments
派生自 IEnumerable
[System.Runtime.InteropServices.Guid("0006303C-0000-0000-C000-000000000046")]
public interface Attachments : System.Collections.IEnumerable
考虑使用实际列表作为内存存储,然后将模拟设置为 return
var list = new List<Outlook.Attachment>();
list.Add(Substitute.For<Outlook.Attachment>());
list.Add(Substitute.For<Outlook.Attachment>());
list.Add(Substitute.For<Outlook.Attachment>());
list.Add(Substitute.For<Outlook.Attachment>());
Outlook.Attachments attachments = Substitute.For<Outlook.Attachments>();
attachments.GetEnumerator().Returns(list.GetEnumerator());
Outlook.MailItem mailItem = Substitute.For<Outlook.MailItem>();
mailItem.Attachments.Returns(attachments);
//...