Save cassettes without any interactions #79
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is often required when writing table-driven tests where some of the test cases don't produce any interactions (like when validation fails) but the setup and test code is the same for every test case. In this case, on a replay these tests then fail because the cassette for the test cannot be found.
Imagine this test code (I'll try to make it as succinct as possible):
Imagine this were testing an HTTP API and some tests would fail validation and not do any calls to the API that's being tested, resulting in no interactions for that cassette.
I have a Makefile target that runs this using
go test -v ./... -updateTestdata
which sets the mode torecorder.ModeRecordOnly
. In CI the mode is then set torecorder.ModeReplayOnly
. Because the cassette isn't there with the current code, it'll fail on those tests. I also decided against using a single cassette for the whole test function because that's brittle and will prevent us from adding more test cases in the middle and will result in one big cassette that can't be grasped easily.Dropping these three lines of code will write a file like this:
…which is perfectly good. Removing this code also doesn't break any tests.
I know, of course, that this is a behavior change. I can also make it configurable but I decided against that for now.