Skip to content

Commit

Permalink
Add start_cassette / stop_cassette macro (not compatible with `:s…
Browse files Browse the repository at this point in the history
…tub`)
  • Loading branch information
marcandre committed Feb 8, 2022
1 parent 69aaa49 commit 262b8ed
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 @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 262b8ed

Please sign in to comment.