[TestMethod]
public void TestMethod1()
{
var mock = new Mock<EmailService>();
mock.Setup(x => x.SendEmail()).Returns(true);
var cus = new Customer();
var result = cus.AddCustomer(mock.Object);
Assert.IsTrue(result);
}
public class Customer
{
public bool AddCustomer(EmailService emailService)
{
emailService.SendEmail();
Debug.WriteLine("new customer added");
return true;
}
}
public class EmailService
{
public virtual bool SendEmail()
{
throw new Exception("send email failed cuz bla bla bla");
}
}
Run Code Online (Sandbox Code Playgroud)
该EmailService.SendEmail方法必须是虚拟的才能模拟它.有没有办法模拟非虚拟方法?
我有一个使用 wsdl.exe 工具生成的 C# 类,如下所示:
public partial class SoapApi : System.Web.Services.Protocols.SoapHttpClientProtocol
{
public SOAPTypeEnum AskServerQuestion()
{
object[] results = return this.Invoke("AskServerQuestion");
return (SOAPTypeEnum) results[0];
}
}
Run Code Online (Sandbox Code Playgroud)
我有一些围绕此的瘦包装器代码,用于跟踪结果等。是否可以使用任何对象模拟框架来制作一个假的 SoapApi 类,并为每个对瘦包装器函数的调用返回可预测的结果?
我无法将 AskServerQuestion() 函数设为虚拟函数,因为它是由 wsdl.exe 工具自动生成的。