Advanced unit testing scenarios
Note: All of these articles are using C# code examples, but you can use the approaches described in any language.
- How to unit test async methods
- How to unit test code that depends on an event
- How to unit test code that uses HttpClient
- How to unit test code that uses Dapper
- How to mock static methods
- How to unit test an ApiController
- How to unit test code that reads and writes to the console
- How to unit test code that does File IO
Unit testing in .NET
- Use StringAssert when testing a string for substrings
- Use DynamicData attribute to pass functions and objects into parameterized tests
- Add [Ignore] to disable a unit test
- Use Assert.ThrowsException instead of ExpectedException attribute
- Parameterized tests with MSTest v2
- Moq – How to return different values each time a mocked method is called
- Parameterized tests in xUnit
- Use FluentAssertions to improve unit tests
- How to use Callback() to capture parameters passed to mocked methods
- Verifying parameters passed to a mocked method
- Quick way to create a unit test project in Visual Studio
- How to unit test an action filter
- How to unit test a model validation attribute
Problems in .NET testing
- Visual Studio 2019: MSTest unit tests are not running in Test Explorer
- Unit test doesn’t finish and stops all other tests from running
- Test fails with message ClassInitialize has the wrong signature
- Can’t pass decimal parameter in DataTestMethod
- Error CS0854 when you’re using Moq with optional parameters
Test-Driven Development (TDD)