Unit Test With xUnit and AutoFixture
AutoFixture is created to minimize the ‘arrange’ phase of your unit tests. Unit Testing: Activate Easy Mode shows me how easy it is to write unit tests when you have the correct toolsets. My demo program follows the same idea, but with my favorite libraries.
AutoFixture.xUnit2.AutoMoq
This project creates an AutoMockDataAttribute
that extends AutoDataAttribute
. It uses Moq
to create a fixture. You can also extends InlineAutoDataAttribute
and MemberAutoDataAttribute
like this.
Cannot Mock Extension Method With Moq
ProductServiceTests shows you how to mock IMemoryCache
. The TryGetValue
method we use in the project is an extension method. If we try to mock it directly, the library will complain. The walk around is to use the native method used by the extension method.
Mock IHttpContextAccessor
AcceptLanguageProviderTests demonstrates how to mock IHttpContextAccessor
.
Unit Test Practices
Unit testing best practices with .NET Core and .NET Standard is a good article to read.