Mastering C# Unit Testing with Real-world Examples
Write readable and maintainable unit tests in C# by refactoring real code examples.
Have you learned the basics of unit testing in C#, but you still struggle with writing tests that are easy to read and maintain?
Have you seen too many online tutorials testing a Calculator class, but you want to learn how to test real code?
Code with dependencies, validations, and edge cases.
Code that looks like the code you find every day at work.
That's why I created this course.
No more unit testing the Add() method of a Calculator class.
In this course, let's refactor real unit tests to make them more readable and maintainable using proven principles and methods.
Although I've changed names, classes, and methods to avoid disclosing code from past clients and employers, these are the tests I had to work with as a software engineer.
Who Is This For?
This course is for C# developers who:
- Know what MSTest, Moq, and AAA mean.
- Want to write readable and maintainable unit tests.
- Are tired of boring, unrealistic tutorials and want to learn from real code examples.
What You’ll Learn
By the end of this course, you’ll be able to:
- Simplify tests with builder methods.
- Write maintainable tests easily with parameterized values.
- Make tests clear and easy to follow with good names and test values.
- Have more readable tests with domain-specific assertion methods.
Mastering these skills will help you to write tests easy to follow, easy to maintain, and easy to trust.
The next time you open one of your tests, you’ll know exactly what’s going on.
Important Note:
This is not an introductory course. We won't cover how to write your first unit tests.
If you're new to unit testing, start with my book Start Testing. It takes you from zero to your first unit tests in C#.
But if you already know what mocks are and when to use them, you're ready for this.
What's Inside
In this course, you will get access to:
- 8 lessons covering different best practices for unit testing.
- 10+ real unit tests that need refactoring.
- 4 hands-on exercises to apply what you've learned.
- 1 checklist summarizing all best practices.
Ready for a preview? Click the video at the top of the page.
These are the modules and lessons inside:
-
Testing AccountController............................4m45s
- Use Builder Methods
- Test IOptions Interface
- Make Test Values Obvious
- (Optional) Check for Missing Configuration Values Inside Constructors
-
Testing PaymentRequestValidator...............3m40s
- Use Parameterized Tests
- Use FluentValidation Helpers
- Use Parameterized Tests Instead of Loops
-
Testing PaymentService.................................4m35s
- Test a Single Concerned Per Test
- Test Exception Messages
-
Testing PayoutService....................................7m17s
- Visually Structure Tests
- (Optional) Use Fakes in Unit Testing
- (Optional) Create Fakes With Moq
- Use TypeBuilder
- Avoid Overspecification
-
Testing DictionaryExtensions.......................7m28s
- Use Simple Test Values
- Exercise: Refactor a Test That Merges Dictionaries
-
Testing SendEmailCommandHandler.........10m26s
- Test Public Behavior
- Create a Custom Verify Method
- Exercise: Refactor a Test That Stores an Email With Duplicated Addresses
-
Testing UpdateStatusCommandHandler..12m11s
- Avoid Hidden Assumptions
- Exercise: Refactor a Test That Updates an Email Status
-
Testing ImportGuestsService.......................4m46s
- Use Object Mothers
- Exercise: Refactor a Test That Uploads Guests
Join "Mastering C# Unit Testing with Real-world Examples"!
Click the "I Want This!" and start writing clean, readable, and maintainable unit tests—the kind of tests your future self and your teammates will thank you for.