我已经设置了2个wcf服务
PdfService.svc
和
MailService.svc
MailService附加PDFService生成的PDF,即
public void SendMail(ICommand command)
{
// how should I handle this in Unit Test (mocks with NSubstitute)
_service = new PdfService(_pdfSettings);
var request = new DownloadRequest {FileName = "form.pdf",
FormEntry = command.FormEntry };
var thefile = _service.DownloadFile(request);
sendEmail(command.Mail, thefile.FileByteStream);
}
我想知道在测试MailService时如何删除PdfService
,这对于将wcf与wcf进行通信是一个坏主意吗?
谢谢!
答案 0 :(得分:0)
您可能想要使用Dependency Injection和Mock Objects。
一般的想法是在方法调用或对象构造函数中将服务传递给您的方法。使用您提供的代码段,我会以这种方式重写:
//Method Call
IPdfService mockService = new MockPdfService() // this is a mock that implements your interface
SendMail(cmd, mockService);
//Method
public void SendMail(ICommand command, IPdfService service)
{
// how should I handle this in Unit Test (mocks with NSubstitute)
_service = new PdfService(_pdfSettings);
var request = new DownloadRequest {FileName = "form.pdf",
FormEntry = command.FormEntry };
var thefile = service.DownloadFile(request);
sendEmail(command.Mail, thefile.FileByteStream);
}
在您的mockService
中,您可以添加诊断帮助,然后您可以在sendmail调用后编写Asserts以查看模拟中发生的事情。
可能更好的方法是让主类构造函数获取服务对象,然后重写方法调用,如下所示:
public class MyClass
{
private IPdfService _pdfService;
public MyClass()
{
_pdfService = new PdfService(_pdfSettings);
}
// Call this with your Mock pdfService
public MyClass(IPdfService pdfService)
{
_pdfService = pdfSerivce;
}
public void SendMail(ICommand command)
{
var request = new DownloadRequest { FileName = "form.pdf", FormEntry = command.FormEntry };
var thefile = _pdfService.DownloadFile(request);
sendEmail(command.Mail, thefile.FileByteStream)
}
}