From beee6137003c56854960b75a9b9214206dbb915a Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Tue, 8 Feb 2022 17:20:40 -0500 Subject: [PATCH] Add `start_cassette` / `stop_cassette` macro [Fixes #185] --- lib/exvcr/mock.ex | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/exvcr/mock.ex b/lib/exvcr/mock.ex index e81579b..518c7fd 100644 --- a/lib/exvcr/mock.ex +++ b/lib/exvcr/mock.ex @@ -54,22 +54,13 @@ defmodule ExVCR.Mock do defmacro use_cassette(fixture, options, test) do quote do - recorder = Recorder.start( - unquote(options) ++ [fixture: normalize_fixture(unquote(fixture)), adapter: adapter_method()]) - + recorder = start_cassette(unquote(fixture), unquote(options)) try do - mock_methods(recorder, adapter_method()) [do: return_value] = unquote(test) return_value after - recorder_result = Recorder.save(recorder) - - module_name = adapter_method().module_name - unload(module_name) - ExVCR.MockLock.release_lock() - - recorder_result + stop_cassette(recorder) end end end @@ -80,6 +71,31 @@ defmodule ExVCR.Mock do end end + defmacro start_cassette(fixture, options) when fixture != :stub do + quote do + recorder = + Recorder.start( + unquote(options) ++ + [fixture: normalize_fixture(unquote(fixture)), adapter: adapter_method()] + ) + + mock_methods(recorder, adapter_method()) + recorder + end + end + + defmacro stop_cassette(recorder) do + quote do + recorder_result = Recorder.save(unquote(recorder)) + + module_name = adapter_method().module_name + unload(module_name) + ExVCR.MockLock.release_lock() + + recorder_result + end + end + @doc false defp load(adapter, recorder) do if ExVCR.Application.global_mock_enabled?() do