wfgilman

wfgilman

I have a behaviour for which I need to implement real functions and mock versions for testing. Right now I am using three modules to do this: the behaviour, the live and the mock. It makes my file structure and naming kind of a pain.

Is something like this possible?

def MyApp.MyModule do
  @behaviour MyApp.MyModule

  @callback myfunction(atom, list) :: :ok | :error
  def myfunction(arg, opts) do
    ...
  end
end

The Ecto.Repo behaviour has something similar where the default function implementations are in a __using__/1 macro, but I don’t need any code injection.

If not, why?

Showing Posts 1 to 10

wfgilman

wfgilman OP

bump :blush:

OvermindDL1

OvermindDL1

Weekends are slow. ^.^

However you should be able to have a module that both defines and implements a behaviour, I used to do it back in the Erlang days for a few ‘default behaviour’ things.

Better question though, why? There are almost always better ways. :slight_smile:

You should never really mock functions, ever, you can mock a process, but if you are mocking functions then something is almost certainly done wrong. What are you trying to actually achieve with a full example we can run ourselves? :slight_smile:

wfgilman

wfgilman OP

I realized that after I submitted the topic late Friday afternoon :joy:

The mocks are for an application that talks to a third party API. I’m trying to implement the application in a way that allows for proper testing.

I built an Elixir library for Plaid, the financial data service. I’m using this application as part of an umbrella application that is my larger project. I’ve since refactored the Plaid application to implement the recommended best practice for testing functions that call a third party. Here is a gist that shows the implementation for the Plaid /connect endpoint refactored to use this best practice. A single endpoint contains three files in the following directory structure:

+ lib
   + plaid
     + connect
       - live.ex
       - mock.ex
     - connect.ex

Plaid.Connect.ex is the behaviour and documentation
Plaid.Connect.Live.ex is the implementation
Plaid.Connect.Mock.ex is the mock

In my dev and test environments I can reference the appropriate module using the config.

This works great, but it seems like it can be refactored to something like I outlined in my first post. I’m so interested in doing this because I’d like to deploy this testing practice in other applications of my umbrella project. All the applications talk to each other, and I need to mock those client API functions in order to write proper unit tests. I’d like to be able to do so in a way that allows me to enforce explicit contracts without creating a complicated directory structure. If I can put the behaviour and implementation in the same module, then I can put all the mocks in another location (like a testing folder) which makes things clearer:

+ lib
  + plaid
    - connect.ex
+ test
  + lib
  + support
    + mock
      - connect.ex

Plaid.Connect.ex is the behaviour and implementation
Plaid.Mock.Connect.ex is the mock

wfgilman

wfgilman OP

Any thoughts on this? :039:

hubertlepicki

hubertlepicki

I use behaviours with my modules exactly the same way, and the reason is also testing. I am using mox for testing and I do declare my behaviours and implementations in the same file. In fact, a behaviour is declared next to the function I am about to write.

I just use the behaviour to make sure my mocks follow the same interface as implementations.

axelson

axelson

Scenic Core Team

Can you show an example of a module implementing it’s own behaviour? Do you get a compilation warning from that?

Qqwy

Qqwy

TypeCheck Core Team

You are not restricted to define only a single module in a single file. What name a file has is also not restricted to be the same as the module name.

Of course, it usually is better for readability/understandability of code to not break these two conventions, but there are some common cases in which it makes sense to do so:

  1. Defining implementations for built-in datatypes in the same file as the definition of the protocol.
  2. Defining a module that does not really exist ‘on its own’ inside (or in the same file) as another module. For instance when defining exception structs, ‘nested’ structs, etc. The given example also falls under this, I think

So I could see just having a single file myapp/module.ex file, which contains

defmodule MyApp.Module.Behaviour do
  ...
end
defmodule MyApp.Module do
 ...
end

But then again, personally I would like having more, shorter, files over longer, more complicated ones. Also, your testing implementation of the behaviour probably should live in e.g. test/support/*.ex files, such that your testing implementation is only loaded in the test environment.

Here is an example of how to tell Mix to add extra files in the test environment, and here you can see that the folder in that particular library contains an Ecto.Repo and some schemas to be used in the testing. :slightly_smiling_face:

axelson

axelson

Scenic Core Team

yeah, two modules would work. Although it could be nice if there was a way to define the behaviour and implement it in the same module (although that might be a bad idea)

hubertlepicki

hubertlepicki

Ah no, I don’t think you can do that. I was thinking about the same file but two modules too, precisely what @Qqwy suggests. Probably you can nest the modules too and have like inner module that defines behavior and outer module definig implementation, something like this (untested):

defmodule MyApp.Module do
   defmodule Behaviour do
    ...
  end
   ...
end

I suspect this may work.

Qqwy

Qqwy

TypeCheck Core Team

Having the same module expose a behaviour and implement it breaks the Single Responsibility Principle, which indeed many software developers see as a Bad Idea™.
I think it has been a conscious choice to not allow this :slightly_smiling_face:.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews