Skip to content

[Tests] ImportServiceCommandTests — MockXmlValidator/MockJsonValidator ignore isValid parameter #738

@Christophe-Rogiers

Description

@Christophe-Rogiers

Severity: Warning
File: tests/Servy.CLI.UnitTests/Commands/ImportServiceCommandTests.cs lines 163–175

Description:
The test class exposes two helpers that are meant to mock the static XML and JSON validators for positive/negative paths, but the isValid and errorMsg parameters are accepted and then discarded. The body invokes TryValidate via reflection with hardcoded arguments and ignores the return value:

private void MockXmlValidator(bool isValid, string errorMsg = null!)
{
    var validator = typeof(XmlServiceValidator);
    validator!.GetMethod("TryValidate")!.Invoke(null, new object?[] { "", null });
    // Use a library like Pose or replace XmlServiceValidator with an interface to mock in real project
}

private void MockJsonValidator(bool isValid, string errorMsg = null!) { /* same */ }

The inline comment admits the helper cannot actually change validator behaviour. Every test that calls MockXmlValidator(false, "something") is therefore not testing the error branch at all; it exercises whatever the real XmlServiceValidator.TryValidate("", null) does and then asserts on messages that were produced for unrelated reasons.

Suggested fix:
Replace the static XmlServiceValidator / JsonServiceValidator callers with an IXmlServiceValidator / IJsonServiceValidator interface (already covered partially by the #430 testability pattern) and inject a Moq mock. Then MockXmlValidator(isValid, errorMsg) can configure the mock's behaviour. Until then, either remove the no-op helpers (so the tests visibly don't cover the invalid path) or mark the relevant tests as skipped with [Fact(Skip="validator is static — cannot simulate invalid input")] so the gap is visible.

Metadata

Metadata

Assignees

Labels

testsChanges to test code and coverage

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions