Last updated: October 19, 2026 · Tested with Moq 4.21.0 and 4.20.72, xUnit 2.9.3, .NET 10 (SDK 10.0.401, runtime 10.0.12), Windows 11 Short answer: Create a mock with new Mock<IMyService>() , tell it what to return with Setup(...).Returns(...) (or ReturnsAsync for async methods), pass mock.Object into the class you test, and check the calls afterwards with Verify(..., Times.Once) . Below is a complete xUnit test suite I ran on Moq 4.21.0, the first Moq release in two years (September 24, 2026), plus the exact exception text for the ten mistakes people hit most. Things I found while testing: Upgrading to Moq 4.21.0 added a compiler warning (CS8602) to the usual ILogger verify code. With warnings as errors, the build fails. On 4.20.72 the same code had 0 warnings. You can't set up or verify logger.LogInformation(...) . It's an extension method, so you verify Log(...) instead. An async method you didn't set up doesn't return null . It returns a completed...
CSharp-Coder
CSharp-Coder.com is a free online resource dedicated to helping both beginner and advanced developers learn and improve their programming skills. The website offers a variety of tutorials, articles, and resources designed to make learning programming easy and fun.