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?
Trending in Questions
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wfgilman
bump
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.
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?
wfgilman
I realized that after I submitted the topic late Friday afternoon
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
/connectendpoint refactored to use this best practice. A single endpoint contains three files in the following directory structure:Plaid.Connect.exis the behaviour and documentationPlaid.Connect.Live.exis the implementationPlaid.Connect.Mock.exis the mockIn my
devandtestenvironments I can reference the appropriate module using theconfig.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:
Plaid.Connect.exis the behaviour and implementationPlaid.Mock.Connect.exis the mockwfgilman
Any thoughts on this?
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
Can you show an example of a module implementing it’s own behaviour? Do you get a compilation warning from that?
Qqwy
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:
So I could see just having a single file
myapp/module.exfile, which containsBut 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/*.exfiles, 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.
axelson
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
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):
I suspect this may work.
Qqwy
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