Skip to content

Commit

Permalink
Respond to comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ploeh committed Jan 30, 2024
1 parent 24f9dd9 commit b72cf97
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions _posts/2023-08-14-replacing-mock-and-stub-with-a-fake.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,38 @@ <h2 id="comments-header">
<div class="comment-content">
<p>
<p>
Hi Mark,
</p>
Hi Mark,
</p>
<p>
Firstly, thank you for another insightful article.
</p>
Firstly, thank you for another insightful article.
</p>
<p>
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).
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.
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?
Your guidance would be much appreciated.
</p>
<p>
(And yes, I remember your advice about framing questions —it's in your 'Code that Fits in Your Head' book! :D )
</p>
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).
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.
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?
Your guidance would be much appreciated.
</p>
<p>
Thanks
</p>
(And yes, I remember your advice about framing questions —it's in your 'Code that Fits in Your Head' book! :D )
</p>
<p>
Thanks
</p>
</p>
</div>
<div class="comment-date">2024-01-15 03:10 UTC</div>
</div>

<div class="comment" id="d0ac4c5ecf444fb2a18b993ca16822de">
<div class="comment-author"><a href="/">Mark Seemann</a> <a href="#d0ac4c5ecf444fb2a18b993ca16822de">#</a></div>
<div class="comment-content">
<p>
Thank you for writing. That's a question that warrants an article or two. I've now published an article titled <a href="/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.
</p>
<p>
I'll post an update here when I also have an answer to your specific question.
</p>
</div>
<div class="comment-date">2024-01-30 7:13 UTC</div>
</div>
</div>

0 comments on commit b72cf97

Please sign in to comment.