C# MOQ正在测试电子邮件服务,无法获取值

C# MOQ正在测试电子邮件服务,无法获取值,c#,unit-testing,moq,abstract-class,C#,Unit Testing,Moq,Abstract Class,我正在尝试测试一个域服务,这是发送电子邮件后,订单已下。这个服务有私有方法,所以我在调用这个私有服务方法的公共接口上调用了一个方法。问题是我似乎无法检查电子邮件的抄送,因为这是在私人方法。 我所知道的唯一一种方法是,如果该值被保存为接口属性等,但它不是。请参阅下面的代码 public int SendConsolidatedDespatchNotifications(int countOfWorkDays) { var sent = 0; var

我正在尝试测试一个域服务,这是发送电子邮件后,订单已下。这个服务有私有方法,所以我在调用这个私有服务方法的公共接口上调用了一个方法。问题是我似乎无法检查电子邮件的抄送,因为这是在私人方法。 我所知道的唯一一种方法是,如果该值被保存为接口属性等,但它不是。请参阅下面的代码

     public int SendConsolidatedDespatchNotifications(int countOfWorkDays)
    {
        var sent = 0;
        var trackings = _despatchLineRepository.GetTrackingWithoutDespatchNotificationInPreviousWorkDays(countOfWorkDays);
        var trackingsWithinOrder = trackings == null
            ? new List<OrderLineTracking>()
            : trackings.Where(dl => dl.DespatchReference != null).ToList();
        trackingsWithinOrder.GroupBy(ot => ot.OrderKey).ForEach(
            ot =>
            {
                if (SendConsolidatedDespatchNotifications(ot))
                {
                    _despatchLineRepository.SetAsSent(ot.Select(ol => ol.DespatchLine));
                    sent++;
                }
            });

        return sent;
    }

     private bool SendConsolidatedDespatchNotifications(IGrouping<int, OrderLineTracking> orderTrackingLines)
    {
        if (orderTrackingLines == null)
            return false;
        if (orderTrackingLines.Key == 0)
            return false;
        if (orderTrackingLines.Any())
        {
            var firstLine = orderTrackingLines.First();
            var allOrderLines = _orderLineRepository.GetOrderLinesByOrderKey(firstLine.OrderKey);
            var partiallyDespatchedLines = FindPartiallyDespatchedLines(orderTrackingLines);
            var notDespatchedLines = FindNotDespatchedLines(allOrderLines, orderTrackingLines);
            return SendConsolidatedDespatchedEmail(firstLine.DespatchReference, orderTrackingLines, partiallyDespatchedLines, notDespatchedLines);
        }

        return false;
    }

    private bool SendConsolidatedDespatchedEmail(
        string poNumber,
        IEnumerable<OrderLineTracking> despatchedLines,
        IEnumerable<OrderLineTracking> partiallyDespatchedLines,
        IEnumerable<OrderLine> notDespatchLines)
    {
        //we just assume that one PO have always just one order
        var firstDespatchedLine = despatchedLines.First();
        var order = firstDespatchedLine.OrderLine.OrderHeader;
        if (order?.Customer == null)
            return false;

        var despatchGroups = new List<DespatchLineGroup>();
        despatchedLines.GroupBy(dl => dl.DespatchReference).ForEach(
            dl => despatchGroups.Add(
                new DespatchLineGroup
                {
                    DespatchReference = dl.Key,
                    DespatchedLines = dl,
                    TrackingWebLink = GetTrackingWebLinkFor(dl.First())
                }));

        var despatchNotificationEmail = new DespatchConsolidatedNotificationEmail(
            order.Customer,
            order,
            despatchGroups,
            CreateNotDespatchedItemsList(partiallyDespatchedLines, notDespatchLines));

        var ccCustomer = _customerRepository.GetByCostCentreIdentifier(order.CostCentreIdentifier, order.Customer.Key);

        var ccOnBasket = ccCustomer?.CostCentre; 

        if (ccOnBasket == null)
        {
            despatchNotificationEmail.To.Add(new EmailAddress(order.Customer.FullName, order.Customer.Login));
        }
        else
        {
            FillInSubaccountDetails(despatchNotificationEmail, ccCustomer, order, order.Customer, ccOnBasket);
        }

        despatchNotificationEmail.PopulateContentWithTags();
        despatchNotificationEmail.SendAfter = firstDespatchedLine.DespatchDate;
        despatchNotificationEmail.Save();

        _log.InfoFormat("Despatch email {0} for {2} sent to {1}", "DespatchConsolidatedNotificationEmail", order.Customer.Login, poNumber);

        return true;
    }

    private void FillInSubaccountDetails(
        EmailTemplate email,
        Customer ccCustomer,
        OrderHeader order,
        Customer masterAccount,
        CostCentre ccOnBasket)
    {
        //send notifications to CostCentre account, which is on basket
        email.To.Add(new EmailAddress(ccCustomer.FullName, ccCustomer.Login));

        if (ccOnBasket.ReceiveNotifications) //send notifications to master only if CC is set so
        {
            email.To.Add(new EmailAddress(masterAccount.FullName, masterAccount.Login));
        }

        if (order.OrderPlacedBy.HasValue) //PD-2140 Sending email to Purchaser as well
        {
            var purchaser = _customerRepository.Get(order.OrderPlacedBy.Value);
            if (purchaser?.Purchaser != null && purchaser.Purchaser.ReceiveNotifications)
            {
                email.To.Add(new EmailAddress(purchaser.FullName, purchaser.Login));
            }
        }


        if ( order.ApprovedBy != null)
        {
            var approver = _customerRepository.Get(order.ApprovedBy.Value);
            if(approver?.Approver != null &&  //has approver and its not MAH
               approver.Approver.ReceiveNotifications)
                email.To.Add(new EmailAddress(approver.FullName, approver.Login));
        }
    }

   //this inherits from EmailTemplate which has save method.
   public class DespatchConsolidatedNotificationEmail : EmailTemplate
     {
    public DespatchConsolidatedNotificationEmail() { }

    public DespatchConsolidatedNotificationEmail(
        Customer customer,
        OrderHeader orderHeader,
        List<DespatchLineGroup> despatchLines,
        List<NotDespatchedLine> notDespatchLines)
    {
        AddEmailData(customer);
        AddEmailData(orderHeader);
        AddEmailData(despatchLines);
        AddEmailData(notDespatchLines);
    }
}
    //below is the save method
    public int Save()
    {
        var manageSave = Configuration.Container.Resolve<IWantToManageSaving>();
        return manageSave.Save(this);
    }
public int sendConsolidatedDispatchNotifications(int countOfWorkDays)
{
var=0;
var trackings=_DispatchLineRepository.GetTracking,在前一个工作日(工作日数)不发送通知;
var trackingswithinoder=trackings==null
?新名单()
:trackings.Where(dl=>dl.dispatchreference!=null).ToList();
GroupBy(ot=>ot.OrderKey).ForEach(
ot=>
{
if(发送合并发送通知(ot))
{
_DispatchLineRepository.SetAsSent(ot.Select(ol=>ol.DispatchLine));
发送++;
}
});
返回发送;
}
私有bool发送合并的发送通知(I分组orderTrackingLines)
{
if(orderTrackingLines==null)
返回false;
if(orderTrackingLines.Key==0)
返回false;
if(orderTrackingLines.Any())
{
var firstLine=orderTrackingLines.First();
var allOrderLines=_orderLineRepository.GetOrderLinesByOrderKey(firstLine.OrderKey);
var partiallyDespatchedLines=findppartiallydespatchedlines(orderTrackingLines);
var NotDispatchedLines=FindNotDispatchedLines(allOrderLines、orderTrackingLines);
返回SendConsolidatedDispatchedEmail(firstLine.DispatchedReference、orderTrackingLines、PartiallyDispatchedLines、NotDispatchedLines);
}
返回false;
}
私人bool发送合并的已发送邮件(
字符串poNumber,
我有无数的发送线路,
我有无数的部分储蓄存款,
IEnumerable notDispatchLines)
{
//我们只是假设一个订单总是只有一个订单
var firstDispatchedLine=DispatchedLines.First();
var order=firstDispatchedLine.OrderLine.OrderHeader;
if(订单?.Customer==null)
返回false;
var dispatchgroups=新列表();
DispatchedLines.GroupBy(dl=>dl.DispatchedReference).ForEach(
dl=>DispatchGroups.Add(
新调度组
{
DispatchReference=dl.Key,
调度线路=dl,
TrackingWebLink=GetTrackingWebLinkFor(dl.First())
}));
var DispatchNotificationEmail=新的DispatchConsolidatedNotificationEmail(
订单,客户,
命令,
调度组,
CreateNotDispatchedItemsList(PartiallyDispatchedLines,NotDispatchedLines));
var ccCustomer=\u customerRepository.GetByCostCentreIdentifier(order.CostCentreIdentifier,order.Customer.Key);
var ccOnBasket=ccCustomer?成本中心;
如果(ccOnBasket==null)
{
DispatchNotificationEmail.To.Add(新电子邮件地址(order.Customer.FullName,order.Customer.Login));
}
其他的
{
填写详细信息(发送通知电子邮件、ccCustomer、order、order.Customer、ccOnBasket);
}
DispatchNotificationEmail.PopulateContentWithTags();
DispatchNotificationEmail.SendAfter=firstDispatchedLine.DispatchedDate;
DispatchNotificationEmail.Save();
_log.InfoFormat(“发送给{1}的{2}的发送电子邮件{0}”,“发送合并通知电子邮件”,order.Customer.Login,poNumber);
返回true;
}
私人无效填充详细信息(
电子邮件模板电子邮件,
客户ccCustomer,
订单头订单,
客户总账户,
成本中心(ccOnBasket)
{
//向购物篮中的CostCentre帐户发送通知
email.To.Add(新的电子邮件地址(ccCustomer.FullName,ccCustomer.Login));
if(ccOnBasket.ReceiveNotifications)//仅当CC设置为时才向主机发送通知
{
email.To.Add(新的电子邮件地址(masterAccount.FullName,masterAccount.Login));
}
if(order.OrderPlacedBy.HasValue)//PD-2140也向买方发送电子邮件
{
var purchaser=\u customerRepository.Get(order.OrderPlacedBy.Value);
if(买方?.purchaser!=空和买方.purchaser.ReceiveNotifications)
{
email.To.Add(新的电子邮件地址(purchaser.FullName,purchaser.Login));
}
}
if(order.ApprovedBy!=null)
{
var approver=\u customerRepository.Get(order.ApprovedBy.Value);
如果(approver?.approver!=null&&//has approver及其非MAH
审批人。审批人。接收通知)
email.To.Add(新的电子邮件地址(approver.FullName,approver.Login));
}
}
//这继承自具有保存方法的EmailTemplate。
公共类DispatchConsolidatedNotificationEmail:EmailTemplate
{
公共发送合并通知电子邮件(){}
公开发送合并通知电子邮件(
客户,,
OrderHeader OrderHeader,
列出发送线路,
列表(发送线路)
{
添加邮件数据(客户);
AddEmailData(orderHeader);
添加邮件数据(发送线路);
添加邮件数据(非发送行);
}
}
//下面是保存方法
公共整数保存()
{
var manageSave=Configuration.Container.Resolve();
返回manageSave.Save(此);
}
注意Email实现了一个抽象类,它是EmailTemplate no
Class c = new Class();
PrivateObject o = new PrivateObject(c);
var whatever = o.Invoke("FillInSubaccountDetails");
Assert.AreEqual(whatever, expected);
// ASSERT

savingManagerMock.Verify(x => x.Save(It.IsAny<EmailTemplate>(
    arg => arg.To.Contains(/* ... */));