Skip to content

Fix MSTEST0058: Replace try-catch-assert with Assert.ThrowsExactly#86

Merged
Malcolmnixon merged 2 commits intomainfrom
copilot/fix-sonar-code-smells
Feb 16, 2026
Merged

Fix MSTEST0058: Replace try-catch-assert with Assert.ThrowsExactly#86
Malcolmnixon merged 2 commits intomainfrom
copilot/fix-sonar-code-smells

Conversation

Copy link
Contributor

Copilot AI commented Feb 16, 2026

Pull Request

Description

Resolved 17 MSTEST0058 code smells where assertions in catch blocks could allow tests to pass incorrectly if no exception is thrown.

Replaced try-catch-assert pattern with MSTest v4's Assert.ThrowsExactly<T>():

Before:

try
{
    Context.Create(["--report"]);
    Assert.Fail("Expected ArgumentException was not thrown");
}
catch (ArgumentException ex)
{
    Assert.Contains("--report requires a filename argument", ex.Message);
}

After:

var ex = Assert.ThrowsExactly<ArgumentException>(() => Context.Create(["--report"]));
Assert.Contains("--report requires a filename argument", ex.Message);

Changes:

  • ContextTests.cs: 15 test methods refactored
  • SonarQualityResultTests.cs: 2 test methods refactored
  • Net reduction: 137 lines of code

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code quality improvement

Related Issues

Pre-Submission Checklist

Before submitting this pull request, ensure you have completed the following:

Build and Test

  • Code builds successfully: dotnet build --configuration Release
  • All tests pass: dotnet test --configuration Release
  • Code produces zero warnings

Code Quality

  • Code formatting is correct: dotnet format --verify-no-changes
  • New code has appropriate XML documentation comments
  • Static analyzer warnings have been addressed

Linting

  • Markdown linter passes: npx markdownlint-cli2 "**/*.md" "#node_modules"
  • Spell checker passes: npx cspell "**/*.{md,cs}" --no-progress
  • YAML linter passes: yamllint .

Testing

  • Added unit tests for new functionality
  • Updated existing tests if behavior changed
  • All tests follow the AAA (Arrange, Act, Assert) pattern
  • Test coverage is maintained or improved

Documentation

  • Updated README.md (if applicable)
  • Updated ARCHITECTURE.md (if applicable)
  • Added code examples for new features (if applicable)
  • Updated requirements.yaml (if applicable)

Additional Notes

Assert.ThrowsExactly<T>() ensures exact type matching (no derived types) and automatically fails if no exception is thrown, eliminating the need for explicit Assert.Fail() guards.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…Exactly

Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Sonar issues with asserts in catch blocks Fix MSTEST0058: Replace try-catch-assert with Assert.ThrowsExactly Feb 16, 2026
Copilot AI requested a review from Malcolmnixon February 16, 2026 01:51
@Malcolmnixon Malcolmnixon marked this pull request as ready for review February 16, 2026 02:00
@Malcolmnixon Malcolmnixon merged commit eba5998 into main Feb 16, 2026
11 checks passed
@Malcolmnixon Malcolmnixon deleted the copilot/fix-sonar-code-smells branch February 16, 2026 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants