public void Test_ExceptionDoesNotStopNotifications_UsingWhenConstraint() { // // Create mocks: // var loggerMock = new Moq.Mock <ILogger>(); loggerMock.Setup(x => x.Error(It.IsAny <Exception>())); var bobMock = new Moq.Mock <IEmployee>(); bobMock.Setup(x => x.IsWorkingOnDate(It.IsAny <DateTime>())) .Returns(true); bobMock.Setup(x => x.GetNotificationPreference()) .Returns(LunchNotifier.NotificationType.Email); var marthaMock = new Moq.Mock <IEmployee>(); marthaMock.Setup(x => x.IsWorkingOnDate(It.IsAny <DateTime>())) .Returns(true); marthaMock.Setup(x => x.GetNotificationPreference()) .Returns(LunchNotifier.NotificationType.Email); DateTime?incomingDate; marthaMock.Setup(x => x.IsWorkingOnDate(It.IsAny <DateTime>())) .Returns((Func <DateTime, bool>)(input => { incomingDate = input; return(input.DayOfWeek != DayOfWeek.Sunday); })); var employeeServiceMock = new Moq.Mock <IEmployeeService>(); employeeServiceMock.Setup(x => x.GetEmployeesInNewYorkOffice()) .Returns(new[] { bobMock.Object, marthaMock.Object }); bool isFirstCall = true; var notificationServiceMock = new Moq.Mock <INotificationService>(MockBehavior.Strict); notificationServiceMock .When(() => isFirstCall) .Setup(x => x.SendEmail(It.IsAny <IEmployee>(), It.IsAny <string>())) .Callback(() => isFirstCall = false) .Throws <Exception>();; notificationServiceMock .When(() => !isFirstCall) .Setup(x => x.SendEmail(It.IsAny <IEmployee>(), It.IsAny <string>())); // // Create instance of class I'm testing: // var classUnderTest = new LunchNotifier(notificationServiceMock.Object, employeeServiceMock.Object, loggerMock.Object); // // Run some logic to test: // classUnderTest.SendLunchtimeNotifications(); // // Check the results: // notificationServiceMock.Verify(x => x.SendEmail(It.IsAny <IEmployee>(), It.IsAny <string>()), Times.Exactly(2)); loggerMock.Verify(x => x.Error(It.IsAny <Exception>()), Times.Once); }