diff --git a/lib/exvcr/mock.ex b/lib/exvcr/mock.ex index b2c5459..e7321b6 100644 --- a/lib/exvcr/mock.ex +++ b/lib/exvcr/mock.ex @@ -57,22 +57,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 @@ -86,6 +77,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