Updating per duplicate tag, the tagged duplicate mostly talks about setting up moq objects using ref or out, not really how to verify them. I see I can use ref It.Ref.IsAny in my verify statement, but I'd like to verify values are set properly, not just that the method was called.
I'm trying to write a test case for some legacy code, a method that calls a repository to do an insert but the repository method takes in the reference to an object. So the basic flow of the method I'm trying to test:
- Call service method in Nunit
- Service method creates an object based on parameters passed in
- Service calls repository to do an insert
- Service method doesn't return anything
Using Moq, I've setup a mocked repository class and I'm able to verify that it actually did the call, but I'd like to be able to verify that it generated the object correctly. I am building an identical object and am attempting the verify call as this:
_mockWorkerRepository.Verify(w => w.Create(ref newWorker), Times.Once);
My test case throws an error that it did not match. Stepping through code I can verify that both objects are the same, but I'm guessing that because I'm passing in the reference, the reference is different? Any thoughts? Thanks!
Update to include full test method and method under test:
[Test]
public void ConfirmWorker_ShouldCreateWorker()
{
//Arrange
Worker worker = null;
var newWorker = new Worker
{
Name = $@"{_machineName.ToLower()} worker",
StatusId = WorkerStatus.OFFLINE,
TypeId = WorkerType.TYPE,
MessageQueueAddress = $@"FORMATNAME:DIRECT=OS:{_machineName}\private$\{_queueName}",
HostProcessIdentifier = _testConfig.HostId,
Created = _testTime,
CreatedBy = "createdBy",
LastUpdated = _testTime,
LastUpdatedBy = "updatedBy",
LastCheckinDate = _testTime
};
_mockWorkerRepository.Setup(w => w.GetByHostId(It.IsAny<string>())).Returns(worker);
_mockDateTimeRepository.Setup(d => d.UtcNow()).Returns(_testTime);
var workerService = new WorkerService(_mockDateTimeRepository.Object, _mockJobRepository.Object, _mockJobWorkerRepository.Object, _mockLogger.Object, _mockQueueRepository.Object, _mockWorkerRepository.Object, _testConfig);
//Act
workerService.ConfirmWorker(_machineName, _queueName);
//Assert
_mockWorkerRepository.Verify(w => w.Create(ref newWorker), Times.Once);
_mockLogger.Verify(l => l.WriteLog("Worker registered in database"), Times.Once);
}
public void ConfirmWorker(string machineName, string queueName)
{
_logger.WriteLog($"Checking to add Worker if didn't exist for: {_configSettings.HostId} with {queueName}");
var worker = _workerRepository.GetByHostId(_configSettings.HostId);
if (worker != null)
{
_logger.WriteLog("Worker exist, skipping creation");
return;
}
_logger.WriteLog("Instance of this worker not found, CREATING one.");
var newWorker = new Worker()
{
Name = $@"{machineName.ToLower()} worker",
StatusId = WorkerStatus.OFFLINE,
TypeId = WorkerType.TYPE,
MessageQueueAddress = $@"FORMATNAME:DIRECT=OS:{machineName}\private$\{queueName}",
HostProcessIdentifier = _configSettings.HostId,
Created = _dateTimeRepository.UtcNow(),
CreatedBy = "createdBy",
LastUpdated = _dateTimeRepository.UtcNow(),
LastUpdatedBy = "updatedBy",
LastCheckinDate = _dateTimeRepository.UtcNow()
};
_workerRepository.Create(ref newWorker);
_logger.WriteLog("Worker registered in database");
}