Back to list
Joshua-Palamuttam

testing-patterns

by Joshua-Palamuttam

0🍴 0📅 Dec 8, 2025

SKILL.md


name: testing-patterns description: Unit testing patterns with xUnit, Moq, and FluentAssertions including AAA pattern, naming conventions, mocking, and edge case coverage. Use when writing or reviewing unit tests.

Testing Patterns

Overview

Tests ensure code correctness and enable confident refactoring. Follow these patterns for consistent, maintainable tests.

Test Framework Stack

  • xUnit - Test framework
  • Moq - Mocking library
  • FluentAssertions - Readable assertions

Test Structure

Arrange-Act-Assert (AAA)

Every test follows this pattern:

[Fact]
public async Task GetByIdAsync_WithValidId_ReturnsTask()
{
    // Arrange
    var taskId = Guid.NewGuid();
    var expectedTask = new TaskItem { Id = taskId, Title = "Test Task" };
    _repositoryMock.Setup(r => r.GetByIdAsync(taskId, It.IsAny<CancellationToken>()))
        .ReturnsAsync(expectedTask);

    // Act
    var result = await _sut.GetByIdAsync(taskId);

    // Assert
    result.Should().NotBeNull();
    result!.Id.Should().Be(taskId);
    result.Title.Should().Be("Test Task");
}

Test Class Structure

public class TaskServiceTests
{
    private readonly Mock<ITaskRepository> _repositoryMock;
    private readonly Mock<ILogger<TaskService>> _loggerMock;
    private readonly TaskService _sut; // System Under Test

    public TaskServiceTests()
    {
        _repositoryMock = new Mock<ITaskRepository>();
        _loggerMock = new Mock<ILogger<TaskService>>();
        _sut = new TaskService(_repositoryMock.Object, _loggerMock.Object);
    }

    // Tests...
}

Naming Conventions

Test Method Names

Format: MethodName_Scenario_ExpectedResult

// Good - descriptive names
public async Task GetByIdAsync_WithValidId_ReturnsTask()
public async Task GetByIdAsync_WithNonExistentId_ReturnsNull()
public async Task CreateAsync_WithValidRequest_CreatesAndReturnsTask()
public async Task DeleteAsync_WithExistingId_ReturnsTrue()

// Bad - unclear names
public async Task TestGet()
public async Task Test1()

Mocking with Moq

Setup Mock Returns

// Return a value
_repositoryMock.Setup(r => r.GetByIdAsync(taskId, It.IsAny<CancellationToken>()))
    .ReturnsAsync(expectedTask);

// Return null
_repositoryMock.Setup(r => r.GetByIdAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
    .ReturnsAsync((TaskItem?)null);

Verify Mock Calls

_repositoryMock.Verify(r => r.CreateAsync(It.IsAny<TaskItem>(), It.IsAny<CancellationToken>()), Times.Once);
_repositoryMock.Verify(r => r.DeleteAsync(taskId, It.IsAny<CancellationToken>()), Times.Once);

FluentAssertions

Basic Assertions

result.Should().Be(expected);
result.Should().BeNull();
result.Should().NotBeNull();
items.Should().HaveCount(3);
items.Should().BeEmpty();
result.IsCompleted.Should().BeTrue();

Edge Case Coverage

Always Test These Scenarios

  • GET: found, not found, empty list
  • CREATE: success, validates input, sets defaults
  • UPDATE: success, not found, partial update
  • DELETE: success, not found

Score

Total Score

50/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon