Skip to content

Commit 77a97ce

Browse files
committed
stict mocks failing inside a try/catch aren't causing tests to fail.
1 parent 23aa769 commit 77a97ce

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎MSFakesExamples/MsFakesTests.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ public void Test_EmployeeInOfficeGetsNotified_UsingStrictMock()
102102

103103
notificationServiceMock.AssertWasCalled(x => x.SendEmail(bobMock, With.Any<string>()));
104104

105+
// Since the notification mock calls are wrapped in a try/catch where the exeception is just
106+
// logged, we need to ensure the error logger was never called due to our strict mock failing.
107+
loggerMock.AssertWasNotCalled(x => x.Error(With.Any<Exception>()));
108+
105109

106110
}
107111

‎TypemockIsolatorExamples/IsolatorTests.cs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void Test_EmployeeInOfficeGetsNotified_UsingStrictMock()
6767
// Create mocks:
6868
//
6969
var loggerMock = Isolate.Fake.Instance<ILogger>();
70-
70+
7171
var bobMock = Isolate.Fake.Instance<IEmployee>();
7272
Isolate.WhenCalled(() => bobMock.IsWorkingOnDate(DateTime.MinValue)).WillReturn(true);
7373
Isolate.WhenCalled(() => bobMock.GetNotificationPreference()).WillReturn(LunchNotifier.NotificationType.Email);
@@ -76,7 +76,7 @@ public void Test_EmployeeInOfficeGetsNotified_UsingStrictMock()
7676
Isolate.WhenCalled(() => employeeServiceMock.GetEmployeesInNewYorkOffice()).WillReturn(new[] { bobMock });
7777

7878
var notificationServiceMock = Isolate.Fake.Instance<INotificationService>(Members.MustBeSpecified);
79-
Isolate.WhenCalled(() => notificationServiceMock.SendEmail(null, null));
79+
Isolate.WhenCalled(() => notificationServiceMock.SendEmail(null, null)).IgnoreCall();
8080

8181
//
8282
// Create instance of class I'm testing:
@@ -95,7 +95,9 @@ public void Test_EmployeeInOfficeGetsNotified_UsingStrictMock()
9595
Isolate.Verify.WasCalledWithArguments(() => notificationServiceMock.SendEmail(null, null))
9696
.Matching(args => ((IEmployee)args[0]).Equals(bobMock));
9797

98-
Assert.Fail("Not working as a strict mock");
98+
// Since the notification mock calls are wrapped in a try/catch where the exeception is just
99+
// logged, we need to ensure the error logger was never called due to our strict mock failing.
100+
Isolate.Verify.WasNotCalled(() => loggerMock.Error(null));
99101

100102
}
101103

@@ -122,7 +124,7 @@ public void Test_ExceptionDoesNotStopNotifications()
122124

123125
var notificationServiceMock = Isolate.Fake.Instance<INotificationService>();
124126
Isolate.WhenCalled(() => notificationServiceMock.SendEmail(null, null)).WillThrow(new Exception());
125-
127+
126128
//
127129
// Create instance of class I'm testing:
128130
//
@@ -172,7 +174,7 @@ public void Test_CorrectTemplateIsUsed_LateLunch(string currentTime, string expe
172174
var notificationServiceMock = Isolate.Fake.Instance<INotificationService>();
173175

174176
Isolate.WhenCalled(() => System.DateTime.Now).WillReturn(DateTime.Parse(currentTime));
175-
177+
176178
//
177179
// Create instance of class I'm testing:
178180
//

0 commit comments

Comments
 (0)