SShortSingh.
Back to feed

Mock HttpMessageHandler, Not Interfaces, for Better HttpClient Testing in .NET

0
·8 views

A common but flawed practice among .NET developers involves wrapping HttpClient behind a custom interface to enable unit testing, which bypasses real serialization, headers, and error handling. Since HttpClient accepts an HttpMessageHandler in its constructor, developers can inject a mock handler directly to intercept and validate outgoing HTTP requests without losing built-in features. This approach allows tests to verify request methods, URIs, status codes, and response deserialization in a realistic way. Helper classes can further simplify test setup by creating pre-configured mock clients for both success and error scenarios. The technique avoids unnecessary abstractions while keeping tests accurate and maintainable.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Kindy AI Avatar Guides Non-Cash Volunteers to Local Giving Opportunities

A developer built Kindy, a voice-driven animated AI companion, as part of a Weekend Generosity Challenge to help people contribute without donating money. Users speak naturally to a 2D cartoon avatar that responds with exaggerated comic emotions and personalized suggestions based on their skills, interests, and available time. Kindy then generates six actionable volunteering ideas and uses Google Maps to surface nearby community centers, food banks, and volunteer spots. The app is powered by Google Gemini AI models for conversation and text-to-speech, with a React frontend handling real-time avatar expressions via SVG animations. It is deployed on Google Cloud Run, with full source code available on GitHub and no user data stored externally.

0
ProgrammingDEV Community ·

Dev Team Shipped Slower Parallel Feature After Benchmark Showed 16% Performance Loss

A developer building logq, a command-line log query tool for a zero-dependency hackathon, added an 8-goroutine parallelization flag despite benchmarks showing it ran 16% slower than a single worker on a 76.3MB test file. The team disclosed the performance gap immediately and shipped the feature regardless, citing its potential value at larger scales. A key design decision was a three-valued evaluator distinguishing between MISSING, null, and false states, preventing query crashes when log records have inconsistent shapes. The project also replaced the widely used third-party library tidwall/gjson with roughly 150 lines of custom code built on Go's standard library, avoiding known issues with float64 precision loss and unpredictable map iteration order. The team removed a CI pipeline two days before submission after it failed for reasons they could not fully diagnose, choosing transparency over leaving an unexplained failure badge.

0
ProgrammingDEV Community ·

Developer finds npm warnings, CI glob quirks, and registry surprises shipping tiny package

A developer publishing a 200-line npm package called video-bitrate-calculator encountered three misleading tooling issues during the release process. An npm warning claimed CLI entry points were deleted during publish, but reading npm's own source code revealed the paths were merely normalized and the entries remained intact. In CI testing, quoted glob patterns in the test script worked on Node 22 but silently matched nothing on Node 18 and 20, meaning newer Node versions masked a real coverage gap rather than catching it. The fix was to drop the glob entirely and let the Node test runner discover test files automatically, ensuring consistent behavior across versions. A third surprise involved npm login unexpectedly opening a signup page for an unfamiliar registry, highlighting how default tooling assumptions can mislead developers into unnecessary or incorrect actions.