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

Allow adding new mocks in Tesla.Mock.mock instead of overwriting previously defined mocks #541

Closed
Nezteb opened this issue Aug 25, 2022 · 1 comment

Comments

@Nezteb
Copy link

Nezteb commented Aug 25, 2022

Note: most of this wouldn't be an issue depending on the results of #241

Say I have a test:

def setup do
  Tesla.Mock.mock(fn ->
    # lots of mocks
  end)
end

# lots of tests

test "a new thing" do
  Tesla.Mock.mock(fn ->
    # a mock to override a previously defined one
    # (to test a different response for the same request,
    # like an error)
  end)
  
  # Now all of my mocks from `setup` are gone
end

I don't know what the API would look like but I have two ideas to not break existing tests:

Option 1:

def setup do
  # `mock` now returns a collection of all mocks
  Tesla.Mock.mock(fn ->
    # lots of mocks
  end)
end

# lots of tests

test "a new thing",  context_mocks do
  Tesla.Mock.mock(context_mocks, fn ->
    # a mock to override a previously defined one
    # (to test a different response for the same request,
    # like an error)
  end)
  
  # Now all of my mocks from `setup` are still there, but one of them is overridden with a newer mock
end

Option 2:

def setup do
  # no change to `mock`
  Tesla.Mock.mock(fn ->
    # lots of mocks
  end)
end

# lots of tests

test "a new thing" do
  Tesla.Mock.add_mock(fn ->
    # a mock to override a previously defined one
    # (to test a different response for the same request,
    # like an error)
  end)
  
  # Now all of my mocks from `setup` are still there, but one of them is overridden with a newer mock
end

Thoughts? 😅

@yordis
Copy link
Member

yordis commented Oct 24, 2024

After #718 I encourage people to migrate to the new Tesla.Test module

@yordis yordis closed this as not planned Won't fix, can't repro, duplicate, stale Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants