Skip to content

CodeQL 11: chore(tests): replace Returns((T?)null) casts with ReturnsNull()#197

Open
rlorenzo wants to merge 3 commits into
mainfrom
codeql/11-nsubstitute-returns
Open

CodeQL 11: chore(tests): replace Returns((T?)null) casts with ReturnsNull()#197
rlorenzo wants to merge 3 commits into
mainfrom
codeql/11-nsubstitute-returns

Conversation

@rlorenzo
Copy link
Copy Markdown
Contributor

@rlorenzo rlorenzo commented May 13, 2026

Summary

Closes ~19 cs/useless-upcast alerts in test/** by replacing the NSubstitute pattern .Returns((SomeType?)null) with the dedicated .ReturnsNull() extension from NSubstitute.ReturnsExtensions.

Why

The explicit (T?)null cast existed because the bare .Returns(null) call is ambiguous between NSubstitute's Returns<T>(T) and Returns<T>(Func<CallInfo, T>) overloads, but the cast itself trips the cs/useless-upcast rule (CodeQL sees the receiver's return type, so the cast is "redundant from the type system's POV"). .ReturnsNull() is the canonical NSubstitute idiom for this case - it disambiguates without a cast and makes the test intent clearer.

Files (18)

  • ClinicalSchedulerTestBase, IntegrationTestBase
  • CourseRelationshipsControllerTests, CoursesControllerTests, EffortIntegrationTestBase, EffortRecordsControllerTests, EffortTypesControllerTests, InstructorsControllerTests, PercentagesControllerTests, PercentAssignTypesControllerTests, ReportsControllerTests, UnitsControllerTests, VerificationControllerTests, VerificationServiceTests
  • RolesTests
  • EmailServiceTests
  • EmergencyContactControllerTests, PhotoGalleryControllerTest

Each file got a using NSubstitute.ReturnsExtensions; added.

Context

Eleventh in the CodeQL N: cleanup series. Earlier task #9 had marked these as wontfix-by-design - that was wrong. ReturnsNull() is the correct fix.

Test plan

  • npm run test:backend - 1946 tests passing
  • Pre-commit lint+test+verify all passed
  • CodeQL workflow on this PR shows the test/Effort/cs/useless-upcast alerts closed

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 81ba747f-e682-4cab-b046-eaf9617e0d04

📥 Commits

Reviewing files that changed from the base of the PR and between 38de1ad and 183283a.

📒 Files selected for processing (18)
  • test/ClinicalScheduler/ClinicalSchedulerTestBase.cs
  • test/ClinicalScheduler/IntegrationTestBase.cs
  • test/Effort/CourseRelationshipsControllerTests.cs
  • test/Effort/CoursesControllerTests.cs
  • test/Effort/EffortIntegrationTestBase.cs
  • test/Effort/EffortRecordsControllerTests.cs
  • test/Effort/EffortTypesControllerTests.cs
  • test/Effort/InstructorsControllerTests.cs
  • test/Effort/PercentAssignTypesControllerTests.cs
  • test/Effort/PercentagesControllerTests.cs
  • test/Effort/ReportsControllerTests.cs
  • test/Effort/UnitsControllerTests.cs
  • test/Effort/VerificationControllerTests.cs
  • test/Effort/VerificationServiceTests.cs
  • test/RAPS/RolesTests.cs
  • test/Services/EmailServiceTests.cs
  • test/Students/EmergencyContactControllerTests.cs
  • test/Students/PhotoGalleryControllerTest.cs

📝 Walkthrough

Walkthrough

Seventeen test files across the codebase are updated to use NSubstitute's ReturnsNull() extension method instead of explicitly casting null values. Each file adds the using NSubstitute.ReturnsExtensions; import and replaces mock return patterns from Returns((Type?)null) to ReturnsNull() for improved readability and consistency.

Changes

NSubstitute ReturnsNull() Migration

Layer / File(s) Summary
Test base classes with user authentication mocking
test/ClinicalScheduler/ClinicalSchedulerTestBase.cs, test/ClinicalScheduler/IntegrationTestBase.cs, test/Effort/EffortIntegrationTestBase.cs, test/Services/EmailServiceTests.cs
Test base classes and service tests that configure null user scenarios now import NSubstitute.ReturnsExtensions and use ReturnsNull() instead of casting nullable AaudUser values in SetupNullUser() and permission-setup methods.
Effort module controller and service tests
test/Effort/CoursesControllerTests.cs, test/Effort/CourseRelationshipsControllerTests.cs, test/Effort/EffortRecordsControllerTests.cs, test/Effort/EffortTypesControllerTests.cs, test/Effort/InstructorsControllerTests.cs, test/Effort/PercentAssignTypesControllerTests.cs, test/Effort/PercentagesControllerTests.cs, test/Effort/ReportsControllerTests.cs, test/Effort/UnitsControllerTests.cs, test/Effort/VerificationControllerTests.cs, test/Effort/VerificationServiceTests.cs
All Effort module test files add ReturnsExtensions import and update mock returns for entity not-found paths (courses, effort types, instructors, percentages, units, verifications, sabbaticals) to use ReturnsNull() instead of explicit nullable casts.
RAPS, Students, and remaining module tests
test/RAPS/RolesTests.cs, test/Students/EmergencyContactControllerTests.cs, test/Students/PhotoGalleryControllerTest.cs
RAPS role tests update database set mocks, Students contact and photo controller tests update user and course mocks, all adding ReturnsExtensions and replacing cast-based null returns with ReturnsNull() for consistency.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: replacing Returns((T?)null) casts with ReturnsNull() across test files to resolve CodeQL useless-upcast alerts.
Description check ✅ Passed The description clearly explains the purpose (closing CodeQL useless-upcast alerts), rationale (ReturnsNull() is the canonical idiom), scope (18 test files), and testing status (1946 tests passing).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codeql/11-nsubstitute-returns

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link
Copy Markdown

Bundle Report

Bundle size has no change ✅

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 42.96%. Comparing base (5949e52) to head (f100be8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #197   +/-   ##
=======================================
  Coverage   42.96%   42.96%           
=======================================
  Files         877      877           
  Lines       51468    51468           
  Branches     4802     4802           
=======================================
  Hits        22113    22113           
  Misses      28831    28831           
  Partials      524      524           
Flag Coverage Δ
backend 43.04% <ø> (ø)
frontend 41.34% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

…051)

PR-scoped gate flagged line 183 as a new xUnit1051 warning because
'.ReturnsNull()' shifted git blame to this commit. The underlying
FindAsync(object?[]?) call lacked a CancellationToken arg. Use the
overload that accepts both, matched via Arg.Any<CancellationToken>().
@rlorenzo
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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