我目前正在 Moq'ing - 或尝试 Moq - 一个将数据插入数据库的类。
我正在模拟两个接口,并通过使用 DI 将它们设置为以下“外观”类的构造函数。
目前我所有的测试都运行并通过了,但我需要一些关于如何设置显示的“SaveServiceMessage”方法的返回布尔值的指导。
目前我调用了无效的“SynchEmployee”方法,在该方法内部调用了“SaveServiceMessage”。在 SaveServiceMessage 中,我正在调用构造函数“SaveMessage”的模拟对象方法,这是在真正的非 Moq 对象中访问数据库的方法。
我需要知道的是,我如何能够 Moq 并获取“SaveServiceMessage”的布尔真或假值。我是Moq'ing的新手,希望你们能指导我。
这是我目前的测试:
[Test]
public void TestSynchroniseEmployeeMethod()
{
var employee = new Employee();
{
employee.AOID = "A1";
employee.ID = 1;
}
var guid = Guid.NewGuid();
var serviceClientMock = new Mock<IService>();
var serviceMessageReposMock = new Mock<IServiceMessageRepos>();
var TestFacade = new Facade(serviceClientMock.Object, serviceMessageReposyMock.Object);
TestFacade.SynchroniseEmployee(employee, guid);
serviceMessageReposMock.Verify(x => x.SaveMessage(It.IsAny<ServiceMessage>()), Times.Exactly(1));
serviceClientMock.Verify(x => x.SendServiceMessage(It.IsAny<ServiceMessage>(), It.IsAny<string>()), Times.Exactly(1));
}
public class Facade
{
IService _serviceMessage;
IServiceRepos _serviceMessageRepos
public Facade(IService serviceMessage, IServiceRepos serviceMessageRepos)
{
_serviceMessage = serviceMessage;
_serviceMessageRepos = serviceMessageRepos;
}
public bool SaveServiceMessage(ServiceMessage message, Guid correlationId, ConversationStatus status)
{
if (message != null)
{
message.CorrelationID = correlationId;
_serviceMessageRepos.SaveMessage(message);
return true;
}
return false;
}
public void SynchEmployee(Employee employee, Guid messageId)
{
var employees = new List<Employee>();
employees.Add(employee);
var message = new ServiceMessage(employee.ID, null, ServiceMessageType.EmployeeSyncRequest, Guid.NewGuid());
message.ID = employee.ID.ToString();
SaveServiceMessage(message, message.CorrelationID, ConversationStatus.NEW);
TransmitServiceMessage(message);
}
}