You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: _posts/2023-08-14-replacing-mock-and-stub-with-a-fake.html
+27-14
Original file line number
Diff line number
Diff line change
@@ -239,25 +239,38 @@ <h2 id="comments-header">
239
239
<divclass="comment-content">
240
240
<p>
241
241
<p>
242
-
Hi Mark,
243
-
</p>
242
+
Hi Mark,
243
+
</p>
244
244
<p>
245
-
Firstly, thank you for another insightful article.
246
-
</p>
245
+
Firstly, thank you for another insightful article.
246
+
</p>
247
247
<p>
248
-
I'm curious about using Fakes and testing exceptions. In scenarios where dynamic mocks (like Moq) are employed, we can mock a method to throw an exception, allowing us to test the expected behavior of the System Under Test (SUT).
249
-
In your example, if we were using Moq, we could create a test to mock the UserRepository's Read method to throw a specific exception (e.g., SqlException). This way, we could ensure that the controller responds appropriately, perhaps with an internal server response.
250
-
However, I'm unsure about how to achieve a similar test using Fakes. Is this type of test suitable for Fakes, or do such tests not align with the intended use of Fakes? Personally, I avoid using try-catch blocks in repositories or controllers and prefer handling exceptions in middleware (e.g., ErrorHandler). In such cases, I write separate unit tests for the middleware. Could this be a more fitting approach?
251
-
Your guidance would be much appreciated.
252
-
</p>
253
-
<p>
254
-
(And yes, I remember your advice about framing questions —it's in your 'Code that Fits in Your Head' book! :D )
255
-
</p>
248
+
I'm curious about using Fakes and testing exceptions. In scenarios where dynamic mocks (like Moq) are employed, we can mock a method to throw an exception, allowing us to test the expected behavior of the System Under Test (SUT).
249
+
In your example, if we were using Moq, we could create a test to mock the UserRepository's Read method to throw a specific exception (e.g., SqlException). This way, we could ensure that the controller responds appropriately, perhaps with an internal server response.
250
+
However, I'm unsure about how to achieve a similar test using Fakes. Is this type of test suitable for Fakes, or do such tests not align with the intended use of Fakes? Personally, I avoid using try-catch blocks in repositories or controllers and prefer handling exceptions in middleware (e.g., ErrorHandler). In such cases, I write separate unit tests for the middleware. Could this be a more fitting approach?
251
+
Your guidance would be much appreciated.
252
+
</p>
256
253
<p>
257
-
Thanks
258
-
</p>
254
+
(And yes, I remember your advice about framing questions —it's in your 'Code that Fits in Your Head' book! :D )
Thank you for writing. That's a question that warrants an article or two. I've now published an article titled <ahref="/2024/01/29/error-categories-and-category-errors">Error categories and category errors</a>. It's not a direct answer to your question, but I found it useful to first outline my thinking on errors in general.
269
+
</p>
270
+
<p>
271
+
I'll post an update here when I also have an answer to your specific question.
0 commit comments