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

Add start_cassette / stop_cassette macro` #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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