Closed the-wondersmith closed 1 year ago
I'm afraid I'll need some of those non-trivial examples to be persuaded that this PR should be accepted 👀
@LukeMathWalker That's fair. TBH, all of the examples I've got in my actual codebase are like... right on the line between trivial and non-trivial. The use case is stemming from what a PITA it's proving to be to write tests for a REST API client library for a service that does publish its API docs as a Postman collection (and therefore has "official" deterministic request/response examples) but that I don't actually have anything to do with. I find myself repeating the same code a lot with the only change being a single variable name.
The tl;dr here is the addition allowed me to abstract a several of the repetitions out, but the whole shebang is still pretty repetitive regardless so this PR may just be my extra gene acting up 🙄. If I can't find some objectively non-trivial use case examples to share within the next few days I'll chalk it up to exactly that and just close the PR. 😅
Issue #, if available:
N/A
Description of changes: -
PR adds a
Default
impl andset_status_code
method to thewiremock::ResponseTemplate
struct. The intention of the change is to support abstracting / encapsulating repetitive response creation into discrete functions. For example, writing tests for a client library whose target API always returns a JSON response of some kind but whose responses are only almost always 200.For example, given:
Some of the repetition in the two response functions can be abstracted into a common function like:
which then allows users to write the two responders above as:
Granted, this example is extremely trivial and doesn't appear to save a ton of code. (Also, yes, I know the
set_body_json
method basically does almost exactly this). In my specific case it does actually end up saving a non-trivial amount of code / boilerplate / repetition, so I figured I couldn't be the only one in that boat¯\(ツ)/¯
.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.