D4no0

D4no0

I was wondering how people usually test projects that have to rely heavily on side-effects.

Imagine I have a genserver that ingests messages from RabbitMQ and has to interact with a mongodb, redis instance and maybe do some http requests afterwards. I currently mock all these interactions, however when writing tests, the amount of mock setup is insane and it is very hard to read, not to mention that it is unclear what is being tested at that point.

I can’t help it but feel that the approach where you mock everything in a single place is wrong, but I cannot understand how you would isolate this functionality, at least without starting to mock internal APIs.

I’ve already used PubSub to make this more manageable in some places, however the problem is that the topology of event passing is not good for places where you want to receive a response back (for example to ACK a AMQP message if processed successfully).

Any thoughts on this?

Showing Posts 1 to 10

mudasobwa

mudasobwa

Creator of Cure

I ended up with my own library rambla (the library named after the street in Barcelona and the name was taken as an allusion to Broadway where Broadway is something one uses to get to the town and Rambla is the opposite, getting data out of a town.)

The main concept is “channels” that might be attached to connections. Channel names mustn’t be unique across connections, and publishing to, say, channel_1 would publish to all the connections (handlers/adapters) having the channel with such a name defined. To define the new connection (handler/adapter,) one should implement Rambla.Handler behaviour, which is already implemented for a bunch of destinations out of the box. Two special handlers, Mock and Stub come with a library.

That architecture allows to use Rambla.publish(:channel_1, %{foo: 42}) and transparently mock/stub it in :test by defining the different set of channels (mock instead of amqp + redis in :prod.)

See testing section in docs for details.

D4no0

D4no0 OP

Nice library!

So from what I understand it works on the same pub/sub principle as PubSub? How do you solve the problem when you require synchronous responses from the channel, does that work?

mudasobwa

mudasobwa

Creator of Cure

Well, no, AFAICT. Channels are configurable yet write-only beasts, there is no sub part. The existing handlers do not support synchronous responses, AFAIR, but writing a specific handler is a no-brainer. One might copy-paste the existing one into the new handler and add something like

@impl Rambla.Handler
def handle_publish(message, options, conn) do
  ...

  case Map.get(message, :synch) do
    pid when is_pid(pid) -> send(pid, ...)
    _ -> :ok
  end
end 

or, alternatively, one can pass a callback to publish/3 and have all the low-level connection stuff to deal with (not recommended, only as a last resort.)

LostKobrakai

LostKobrakai

Imo the most important thing is figuring out what part of the whole piece you are interested in testing. Is it that the correct requests are made? Is it that the statemachine built in the genserver moves between state as expected? Are there some calculations/data transformations that need to be correct? If the answer is “everything” than it might be time to split the problem up into smaller pieces.

D4no0

D4no0 OP

Splitting runtime constructs that are expected to be a single unit is not trivial, hence why the question was asked.

Theoretically this could be split in multiple processes, but this also makes error propagation/handing a lot more complicated.

LostKobrakai

LostKobrakai

Splitting this into more processes was not what I was suggesting. Again I’m wondering what exactly you’re trying to test, because that would inform the best way of going forward. I can’t see how a multi step process involving multiple external systems couldn’t be broken down into simpler to test parts.

D4no0

D4no0 OP

You got a good point.

I think what I am trying to say is that I don’t want to have to define separated mocks to test the bigger functionality.

For example let’s suppose we have the following scenario: (genserver) —calls–> do_some_redis_operation().

Let’s suppose I have already tests written for do_some_redis_operation() with the correct mocks, it seems that I have to define a set of mocks again for the tests of genserver. Arguably, these definitions could be in a single place, however this doesn’t change the fact that I have to define the mocks once again, this makes me very nervous about having to maintain the set of mocks for all these situations.

With a pub/sub system, you basically don’t have to do that as you are separating the parts by events, even though there are lots of downsides by doing this too.

I think I will reconsider using mocks for anything but the http API, as it seems to be creating more problems than it solves.

LostKobrakai

LostKobrakai

You’re still only explaining what you do, not what the intent of the test is. If do_some_redis_operation() is tested, what part of (genserver) —calls–> do_some_redis_operation() do you care about?

bgoosman

bgoosman

I’ve been meaning to read this forever. Maybe it’s helpful? James Shore: Testing Without Mocks: A Pattern Language

D4no0

D4no0 OP

The main intent of the test is OFC to check that do_some_redis_operation() is called and other behavior that is being done by the genserver.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews