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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews