Billing unit tests are currently mocked using Mockito, which overrides an entire set of nested function calls. This PR substitutes this with a new mocking utility that only mocks the HTTP request aspect of the tested function, allowing true execution of other aspects (request prep and response handling) of the function stack.
Rate filtering (lowest rate, lowest SmartRate) utilizes real data from the API. Depending on the day when these cassettes are re-recorded, the lowest rate returned by the API call may differ, requiring devs to update the unit test assertions. By mocking the API call, we can still test the local filtering function (what is actually being tested), but with consistent data without needing to adjust assertions.
This PR:
Introduces a new MockClient class that, rather than executing a real HTTP request, finds a matching configured request (via HTTP method and endpoint regex) and returns a configured response (status code and content)
Reworks stacktrace (client -> service -> requestor is now client -> service -> client -> requestor), allowing MockClient to inject itself into the flow via override to execute as required.
A publicly-accessible request function now exists on an EasyPostClient instance. End-users could use this to manually call any endpoint with raw parameters rather than calling it via a specific function (opens up extensibility)
No breaking changes introduced for end-users
Add mocking capabilities to the test suite's VCR utility. When provided with mocked requests, the VCR client for any given unit test will first attempt to return one of the configured mock requests, falling back to the typical VCR if not found. This allows users to continue to use VCR as usual, but mock only certain requests (e.g. mock one specific API request, but use the normal VCR for the other five in this particular unit test).
Implement mocking for billing and rate filtering-related unit tests
Testing
All unit tests pass as expected
Manually confirmed that mock client is being executed via override as expected
Mock requests are not recorded to cassette (since they do not make it to the VCR stage). Technically some cassettes now have unneeded recording entries, but cassettes have not been re-recorded.
Pull Request Type
Please select the option(s) that are relevant to this PR.
[ ] 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)
[x] Improvement (fixing a typo, updating readme, renaming a variable name, etc)
Description
Some of our unit tests can benefit from mocking:
This PR:
MockClient
class that, rather than executing a real HTTP request, finds a matching configured request (via HTTP method and endpoint regex) and returns a configured response (status code and content)request
function now exists on anEasyPostClient
instance. End-users could use this to manually call any endpoint with raw parameters rather than calling it via a specific function (opens up extensibility)Testing
Pull Request Type
Please select the option(s) that are relevant to this PR.