Migration Guide
This guide helps you migrate between different versions of SMock and provides guidance for upgrading from other mocking frameworks.
Table of Contents
Version Migration
Upgrading to Latest Version
When upgrading SMock, always check the release notes for breaking changes.
Package Update Commands
# Package Manager Console
Update-Package SMock
# .NET CLI
dotnet add package SMock --version [latest-version]
# Check current version
dotnet list package SMock
Upgrading from Other Mocking Frameworks
From Moq
SMock can complement Moq for static method scenarios. Here's how to migrate common patterns:
Basic Mocking
// Moq (interface/virtual methods only)
var mock = new Mock<IFileService>();
mock.Setup(x => x.ReadFile("test.txt")).Returns("content");
// SMock (static methods)
using var mock = Mock.Setup(() => File.ReadAllText("test.txt"))
.Returns("content");
Parameter Matching
// Moq
mock.Setup(x => x.Process(It.IsAny<string>())).Returns("result");
// SMock
using var mock = Mock.Setup((context) => MyClass.Process(context.It.IsAny<string>()))
.Returns("result");
Callback Verification
// Moq
var callCount = 0;
mock.Setup(x => x.Log(It.IsAny<string>()))
.Callback<string>(msg => callCount++);
// SMock
var callCount = 0;
using var mock = Mock.Setup((context) => Logger.Log(context.It.IsAny<string>()))
.Callback<string>(msg => callCount++);
Exception Throwing
// Moq
mock.Setup(x => x.Connect()).Throws<ConnectionException>();
// SMock
using var mock = Mock.Setup(() => DatabaseHelper.Connect())
.Throws<ConnectionException>();
From NSubstitute
// NSubstitute (interfaces only)
var service = Substitute.For<IDataService>();
service.GetData("key").Returns("value");
// SMock (static methods)
using var mock = Mock.Setup(() => StaticDataService.GetData("key"))
.Returns("value");
// NSubstitute - Parameter matching
service.GetData(Arg.Any<string>()).Returns("value");
// SMock - Parameter matching
using var mock = Mock.Setup((context) => StaticDataService.GetData(context.It.IsAny<string>()))
.Returns("value");
From Microsoft Fakes (Shims)
Microsoft Fakes Shims are similar to SMock but with different syntax:
// Microsoft Fakes Shims
[TestMethod]
public void TestWithShims()
{
using (ShimsContext.Create())
{
System.IO.Fakes.ShimFile.ReadAllTextString = (path) => "mocked content";
// Test code here
}
}
// SMock equivalent
[Test]
public void TestWithSMock()
{
using var mock = Mock.Setup((context) => File.ReadAllText(context.It.IsAny<string>()))
.Returns("mocked content");
// Test code here
}
Key Differences:
- SMock uses familiar lambda syntax like other modern mocking frameworks
- SMock supports both sequential and hierarchical APIs
- SMock has built-in parameter matching with
Itclass - SMock works with any test framework, not just MSTest
Getting Help with Migration
If you encounter issues during migration:
- Check Release Notes: Review the specific version's release notes for known issues
- Search Issues: Check GitHub Issues for similar problems
- Community Support: Ask in GitHub Discussions
- Create Issue: If you find a bug, create a detailed issue with:
- Source and target versions
- Minimal reproduction code
- Error messages and stack traces
- Environment details (.NET version, OS, etc.)
Post-Migration Best Practices
After successful migration:
- Run Full Test Suite: Ensure all tests pass with the new version
- Performance Testing: Compare test execution times before and after
- Code Review: Review mock setups for optimization opportunities
- Documentation Update: Update team documentation with new patterns
- Training: Share new features and patterns with your team
This migration guide should help you smoothly transition between SMock versions and from other mocking frameworks. For additional support, consult the troubleshooting guide.
Working Migration Examples
The migration examples shown in this guide are based on actual working test cases. You can find complete, debugged migration examples in the SMock test suite:
- Migration Examples -
src/StaticMock.Tests/Tests/Examples/MigrationGuide/MigrationExamples.cs
These examples demonstrate:
- Current working syntax - All examples compile and pass tests with the latest SMock version
- Best practices - Proper usage patterns for both Sequential and Hierarchical APIs
- Real-world scenarios - Practical migration patterns you can copy and adapt
- Parameter matching - Up-to-date syntax for
It.IsAny<T>()and other matchers
Running Migration Examples
# Navigate to the src directory
cd src
# Run the migration examples specifically
dotnet test --filter "ClassName=MigrationExamples"
# Or run all example tests
dotnet test --filter "FullyQualifiedName~Examples"