1

我有一个使用以下方法的 WCF 服务:

Public Function ScheduleEmail(ByVal request As ScheduleEmailRequest) As     ScheduleEmailResponse _
    Implements EmailProtocol.ISchedulingService.ScheduleEmail

    Try
        If Not Email.IsValidEmailAddress(request.EmailAddress) Then
            EmailSchedulerTrace.Source.WriteError(String.Format("Email with template   '{0}' was not sent to '{1}' because it the address is invalid.", request.EmailName, request.EmailAddress))
        Else
            Dim mgr As New JobManager
            Dim job As New EmailJob
            Dim suppression As New SuppressionManager

            Dim emailItem As Email = Email.GetEmailByName(request.EmailName)
            If suppression.CheckSuppresion(emailItem, request.EmailAddress) Then
                job.JobGuid = Guid.NewGuid
                job.EmailAddress = request.EmailAddress
                job.EmailGuid = emailItem.ID
                job.ScheduledSendTime = request.ScheduledTime
                job.CustomAttributes = request.CustomAttributes
                job.ConsumerID = Email.GetConsumerId(request.CustomAttributes)

                mgr.ScheduleJob(job)
            Else
                EmailSchedulerTrace.Source.WriteWarning(String.Format("Email with template '{0}' was not sent to '{1}' because it was suppressed.", request.EmailName, request.EmailAddress))
            End If

        End If

    Catch ex As Exception
        EmailSchedulerTrace.Source.WriteError(ex)
        Throw
    End Try

    Return New ScheduleEmailResponse

End Function

我需要为此方法编写单元测试。请帮我解决

  • 我需要更改我的方法中的任何内容吗?
  • 我应该嘲笑什么?

非常感谢您的帮助。提前致谢。问候, 萨钦

4

1 回答 1

0

您需要能够交换new连接到其他系统(数据库、电子邮件服务器等)的任何“服务”(您在方法中的类或类中的字段),因此您需要interface为类创建 s 并注入在运行时和单元测试中正确实现,您可以创建mockfake实现用于测试目的。

一个好的开始是定义一个接口:

  • JobManager
  • EmailSchedulerTrace
  • SuppressionManager

您可能还需要将静态方法的功能移到Email

  • GetEmailByName
  • GetConsumerId

如果它们封装了数据库访问或您无法隔离的任何其他服务。

于 2013-08-02T11:09:42.290 回答