Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out "unplayed" Interactions? #91

Closed
SilverMaiden opened this issue Mar 5, 2024 · 6 comments
Closed

Filter out "unplayed" Interactions? #91

SilverMaiden opened this issue Mar 5, 2024 · 6 comments

Comments

@SilverMaiden
Copy link

SilverMaiden commented Mar 5, 2024

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!

@dnaeon
Copy link
Owner

dnaeon commented Mar 6, 2024

@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!

@SilverMaiden
Copy link
Author

SilverMaiden commented Mar 6, 2024

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!)

@dnaeon
Copy link
Owner

dnaeon commented Mar 7, 2024

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!

@SilverMaiden
Copy link
Author

SilverMaiden commented Mar 8, 2024

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!

@dnaeon
Copy link
Owner

dnaeon commented Mar 8, 2024

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.

@SilverMaiden
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants