I am not sure what I am doing incorrectly with this setup method.
var mockLogger = new Mock<ILogger>();
var container = new UnityContainer()
.RegisterInstance(mockLogger.Object);
var action = new MockPublishAction(container, ((d1) => { throw new ServiceAgentException("CriticalReason", "CriticalService", "CriticalOperation", true); }));
var mockAction = new Mock<PublishActionBase>(container);
mockAction.Protected().Setup<OperationReport>("PublishHourEnding", It.IsAny<DateTime>()).Returns((DateTime d) =>
{
throw
new ServiceAgentException
("CriticalReason",
"CriticalService",
"CriticalOperation",
true);
});
var hourEndings = new List<DateTime>();
hourEndings.Add(new DateTime(2011, 1, 1, 1, 0, 0));
hourEndings.Add(new DateTime(2011, 1, 1, 2, 0, 0));
hourEndings.Add(new DateTime(2011, 1, 1, 3, 0, 0));
// action
var operationList = mockAction.Object.PublishGenerationRecords(hourEndings);
To help understand the code. PublishGenerationRecords calls PublishHourEnding in a loop, and I want PublishHourEnding to immediately throw a ServiceAgentException, so that the try-catch in PublishGenerationRecords will catch it, and process it correctly(which is what I am testing). I also tried using .Throws when setting up PublishHourEnding, but it has had the same issue. The issue is the exception is not being thrown when calling PublishHourEnding.
Any help would be awesome. Side question why is the Setup when using Protected() not the same syntax as the original setup (lambda argument).