Skip to content

stub_with/2 can only be used inside of a test #41

Closed
@AaronJamesKing

Description

Hello!

I was reading through the documentation and found stub_with/2, which seems like it would be very useful for e.g. implementing both unit test and integration tests within the same test environment.

The documentation gives an example in which stub_with/2 is called directly after defmock/2, but this doesn't seem possible. Here's a small project to demonstrate this. I'm using a test/support/mocks.ex file, but stub_with/2 can't be used in there. That makes sense to me. But then I tried putting it in test_helper.exs, and even inside example_test.exs (outside of the test), but neither of those work either. The function only works when it's used inside of the test macro.

I guess this makes sense from a process perspective, but it seems like I shouldn't have to call stub_with/2 during every test. Is there any way around this at all? The only reason I'm concerned about this is that, in a large project, each integration test would have to stub_with/2 at least a few modules. I'd rather just stub_with/2 all necessary modules at once.

As a side note: am I going about this completely wrong? This is just one of many issues I've encountered while trying to set up unit test and integration tests and acceptance tests altogether. Using the generally-accepted Application.get_env/3 pattern to set up unit tests seems to make other kinds of test very difficult.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions