Agent skill
moq
Creates mock objects and configures test dependencies with Moq. Use when: writing unit tests that need to isolate dependencies, verify method calls, or simulate service behavior.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/moq
SKILL.md
Moq Skill
Moq is the mocking framework used throughout VanDaemon for unit testing. All services are registered as singletons and depend on interfaces, making them ideal candidates for mocking. Tests use Mock<T> to create test doubles, Setup() to configure behavior, and Verify() to assert interactions.
Quick Start
Basic Service Mock
// Mock a service interface
var mockTankService = new Mock<ITankService>();
mockTankService
.Setup(x => x.GetAllTanksAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<Tank> { new Tank { Id = Guid.NewGuid(), Name = "Fresh Water" } });
var controller = new TanksController(mockTankService.Object);
Mock with Argument Matching
var mockControlService = new Mock<IControlService>();
mockControlService
.Setup(x => x.SetControlStateAsync(
It.Is<Guid>(id => id != Guid.Empty),
It.IsAny<object>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
Key Concepts
| Concept | Usage | Example |
|---|---|---|
Mock<T> |
Create mock instance | new Mock<ITankService>() |
.Object |
Get mocked instance | mock.Object |
Setup() |
Configure method behavior | .Setup(x => x.Method()) |
Returns() |
Sync return value | .Returns(value) |
ReturnsAsync() |
Async return value | .ReturnsAsync(value) |
It.IsAny<T>() |
Match any argument | It.IsAny<CancellationToken>() |
It.Is<T>() |
Match with predicate | It.Is<Guid>(g => g != Guid.Empty) |
Verify() |
Assert method called | .Verify(x => x.Method(), Times.Once) |
Common Patterns
Mocking Logger (Serilog via ILogger<T>)
var mockLogger = new Mock<ILogger<TankService>>();
var service = new TankService(mockLogger.Object, mockFileStore.Object);
// Verify logging occurred (Moq can't verify extension methods directly)
// Use LoggerFactory or just trust the implementation
Mocking JsonFileStore
var mockFileStore = new Mock<IJsonFileStore>();
mockFileStore
.Setup(x => x.LoadAsync<List<Tank>>("tanks.json", It.IsAny<CancellationToken>()))
.ReturnsAsync(testTanks);
Mocking SignalR Hub Context
var mockHubContext = new Mock<IHubContext<TelemetryHub>>();
var mockClients = new Mock<IHubClients>();
var mockGroup = new Mock<IClientProxy>();
mockHubContext.Setup(x => x.Clients).Returns(mockClients.Object);
mockClients.Setup(x => x.Group("tanks")).Returns(mockGroup.Object);
See Also
- patterns
- workflows
Related Skills
- See the xunit skill for test structure and assertions
- See the fluent-assertions skill for readable assertions
- See the csharp skill for async/await patterns used with mocks
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?