Skip to content

Suggestion: Provide an easier way to call the original function againΒ #112

@PragTob

Description

@PragTob

First of all, thanks for mimic. I've been wanting to use it for a while, but convincing people to switch mocking libraries always proved hard. Finally have it in a project, and it's as easy and great as I expected it to be. I love mox and the idea, but sometimes the behavior definition etc. felt just like too much ceremony. Mimic strikes a great middle ground, thank you!

Anyhow, this feature request is probably best illustrated by the test I wanted to write. Essentially:

  • mock email sending is failing, see job fails
  • run job again, but this time succeeding and at best calling the original so I can assert the email was sent

Excerpt from test:

    Mimic.expect(Mailer, :deliver, fn _ -> {:error, "Mailer failed"} end)

    # we don't want it to exhaust retries
    drain_jobs(with_recursion: false)
    assert_no_email_sent()

    # Remove the mock/call original - sadly there is no direct way to do this
    Mimic.expect(Mailer, :deliver, fn args ->
      Mimic.call_original(Mailer, :deliver, [args])
    end)

    # Now drain jobs including scheduled/retryable ones
    drain_jobs(with_scheduled: true)

    assert_email_sent(fn email ->
      # ...
    end)

So, what I want to do is clearly and easily possible with:

    Mimic.expect(Mailer, :deliver, fn args ->
      Mimic.call_original(Mailer, :deliver, [args])
    end)

(or well stub)

The suggestion here is that, at least in my mind, this is common enough to warrant its own API but I may be wrong and admit that I'm testing a somewhat more special case where I only want to mock for one scenario/not all.

Something like Mimic.restore_original(mock) and or Mimic.restore_original(mock, function) would be a nice API imo. Of course, function name pending (I was thinking of remove_mock first, but it should also remove stubs and that's weird so restore_original seemed nice).

Anyhow, thanks for your consideration - feel free to say no as you'd be the one ending up having to maintain it (I might give implementing it a shot).

And again, thanks a lot for mimic!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions