Skip to content

Commit

Permalink
Add start_cassette / stop_cassette macro [Fixes parroty#185]
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Apr 17, 2023
1 parent c6bfe6f commit beee613
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lib/exvcr/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit beee613

Please sign in to comment.