dnaeon / go-vcr

Record and replay your HTTP interactions for fast, deterministic and accurate tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filter out "unplayed" Interactions?

SilverMaiden opened this issue · comments

Hi! When replaying interactions for tests, I want to be able to throw some sort of error if there are any Interactions left unplayed (aka the flow of the test has changed, and therefore the Interactions need to be rerecorded) - my initial thought was to filter based on the replayed value, except it's private. With the package as it is now, what would be the recommended way to check the Interactions in a Cassette for replayed=false, or do the equivalent of that?

I appreciate the help, and thank you for your awesome work on this package!

@SilverMaiden , I've just pushed a new commit, which exposes the internal replayed field via WasReplayed method.

7577423

Let me know how it goes.

Thanks!

Thank you for getting back to me so quickly, really appreciate it! This is great - however, I realize now that for my use-case, I need to have access to the loaded Cassette in the Recorder to make use of the method (which we can't get right now). That, or if there was a version of the "BeforeSaveHook" that could be used on "Stop", then I could check the Interactions using WasReplayed (unless I'm missing something, in which case advice here would be great, thank you!)

Hey @SilverMaiden ,

Can you please provide a sample code illustrating what you want to achieve? That would help me a lot to understand your use case.

Thanks!

Sure! Here's an something along the lines of what I'd like to do once a Recorder has finished playing back Interactions (if we went the "exposing Cassette" route:

recorder <- Recorder after playing some Interactions

for _, interaction := range recorder.Cassette.Interactions {
        if !interaction.WasReplayed() {
            return error
        }
}

Basically, after using Recorder to play a Cassette and mock requests for a test, I want to be able to check the Cassette at the end of the test and see if there are any Interactions that weren't played. Similar to the "BeforeSaveHook" that loops through all the Interactions when we use recorder.Stop(), we could have an "AfterStopHook" (pardon the naming, can't think of something better off the top of my head) for when we call recorder.Stop() in a Mode other than "record". I think having a hook might be cleaner than exposing the Cassette itself.

So ideally we'd be able to do something like this:

func CheckForUnplayedHook(i *cassette.Interaction) error {
    if !i.WasReplayed() {
        return error
    }
}
// Elsewhere in the code...
if recorder.Mode() == recorder.ModeReplayOnly {
    r.AddHook(CheckForUnplayedHook, recorder.AfterStopHook)
}

And again - I so appreciate you getting back to me so quickly, this is a fantastic package and I'm excited to be using it!

Hey @SilverMaiden ,

Any chance you can test the code in this PR?

#92

Make sure to check the error returned by Recorder.Stop() method, as this is where any error from OnRecorderStopHook hooks will be propagated back to the caller.

The OnRecorderStopHook + WasReplayed is exactly what I needed, working wonderfully. Thank you!! <3