Fl4m3Ph03n1x

Fl4m3Ph03n1x

Good Elixir TDD resources?

Background

After following the communitiy suggestion, I bought the Elixir in Action 2nd Edition book and I am about to finish it now.

I must say it really helped me out and I am quite glad I bought it. Now I need to look forward towards my next endeavor - testing.

Why testing?

Due to the nature of my work, I do TDD, which is of huge help to me. In my new job using Elixir I can see that TDD also is a good fit for the company and for it’s projects so now I need a way to learn on how to do TDD with Elixir.

What am I looking for?

I am looking for a book, a video course or any set of resources that can teach me how to do TDD the Elixir way. Paying is not an issue as long as it falls bellow the 20 euros mark ( or 25 dollars ).

You guys know this community and its resources the best, what would you recommend?

Most Liked

idlehands

idlehands

Author of Testing Elixir

Andrea Leopardi and I are currently writing a book on testing elixir for PragProg. While it does not emphasize TDD, I’m a strict test-driver. I’m not sure what resources need to be dedicated specifically to TDD if you are already familiar with the concepts from another language.

Let me know how I can help and I’m happy to. I do not know of any specific guides or posts.

kokolegorille

kokolegorille

You also have ExUnit Official Documentation to start with.

I do not know a book dedicated to TDD in Elixir, but testing is not an issue for me, it’s more like a pleasure after using RSpec/Rails. It’s really damn fast. You don’t even have time to get a coffee while running the full tests :slight_smile:

But You need to test different things, like macro testing, OTP testing (You can assert that a process has/has not being called).

Others resources You might find useful.

You might ping @peerreynders for a wise advice on the subject.

peerreynders

peerreynders

I’m not exactly sure what you are expecting.

GOTO 2015 • Agile is Dead • Pragmatic Dave Thomas:

(I seem to recall that he stated of typically having about 60% test coverage.)

Anyway - TDD is a practice and if you have a competent testing framework like ExUnit you should be good to go - what other language specific support do you expect? And thanks to the Erlang community there is PropEr to replace some tedious static test cases.

Keep in mind that any practice should not be taken to the extreme of religion or dogma - that is counter-productive.

Practice TDD when it makes sense but keep in mind that it is just as important to delete those tests once they taught you all they can so that you don’t have to pay the cost of maintaining them (and don’t make the mistake of skipping Refactor in: Red, Green, Refactor - Why Refactor? - Economics).

DevTernity 2017: Ian Cooper - TDD, Where Did It All Go Wrong

cdegroot

cdegroot

Nope.

You can blame Steve Freeman for that answer, he planted it in my mind. We had a coffee (or, probably more likely, a beer) and were chatting about JMock (created by him and others to basically help out with “London School” style TDD-ing) versus all the other mocking libraries and frameworks that were out there.

A lot of users of the other frameworks mocked (couldn’t resist) JMock for being so “primitive”, sometimes going ad hominem, blaming its authors for not understand enough Java to do a “proper” implementation.

Knowing Steve, I knew that that was false. So I brought it up and he said that JMock was intentionally kept simple, and they tried to tune its functionality to do one thing:

Drive good designs.

The goal is that if you cannot JMock it, that’s not a library limitation, it’s a design smell in your code and you need to go and fix it. The other libraries don’t help you there, as they will allow you to mock pretty much anything under the sun (he advised that you should use JMock for your own code and grab one of the other ones in case you need to mock library stuff, because most libraries in Java are just horribly written).

That made something click in my mind, and since then - I think it was a bit under ten years ago - I’ve been looking at how people test in various languages; and they’re on to something. The more powerful the tools, the worse the test suites and the code design (Rails tops it; never, ever, listen to people called DHH for any programming advice). These libraries and frameworks can just walz through anything; they’re akin to just using heavy powertools while trying to learn fine woodworking.

I think that ExUnit is sufficient; the only thing that’s usually hard to test is a GenServer, or Agent, etc; either I call the server-side methods directly, or I pull the business logic out and then the GenServer itself just forwards messages and then becomes “obviously correct” - a term I use for code that doesn’t need testing. Dependencies can be passed in, it usually is not a lot of work. Most of the tools, like ex_mock, come with serious drawbacks and let you get away with murder. Don’t give in to their temptation :wink:

peerreynders

peerreynders

At the risk of stating the obvious:

  • When dealing with function values nothing much changes. Pass in your fake function instead of the real function and have the fake do what it needs to do.

  • However Elixir doesn’t have Objects. In JavaScript Objects conflate (mutable) state and behaviour. In Elixir state is stored in immutable data structures while behaviour resides in modules.

So it’s:

someValue = state.method(...params); // spread params array over arguments

versus

new_state = Module.function(old_state, params) # params is a parameter key list

So in Elixir the “fake object” situation may require a fake data structure (though at times you can simply use the real one) and will require a fake module, which means that your mockable boundary cannot hard code the module so that we can write:

new_state = apply(Module, :function, [old_state, params])

with the rough JavaScript equivalent:

someValue = (state.method).apply(state, params);

Now in Elixir that Module value has to come from somewhere and typically that is resolved at compile time (something that JavaScript doesn’t have) by grabbing the Module from the compilation environment - i.e. compilation in preparation for running tests versus running the application.

This is essentially what Mocking and Explicit Contracts and Mox are based on.

Consequently choosing mockable boundaries in Elixir is a much more deliberate process (hence “Explicit Contracts”) than it typically is in JavaScript with the ad hoc creation of injectable spies (where state has customized behaviour already bound to it).

(Another example)

Where Next?

Popular in Chat/Questions Top

satoru
I’m working on the “Bob” exercise on the Elixir Track in Exercism. I am testing for uppercase letter with this simple check: c in ?A..?Z...
New
maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size fro...
New
ca1989
Hi all, is there any up to date resource out there (blog, talk, video, book…) about deploying elixir applications using releases? In pa...
New
Allyedge
Hey, I want to learn Elixir OTP and I wanted to know if there are any good resources that teach it. I found some web pages, but none of t...
New
gouvermxt
I just finished the “Learn Functional Programming with Elixir (Pragprog)” book. I have 5+ years of experience with Ruby/Rails, my goal is...
New
miguelsrrobo
hi i was wondering if it is necessary to learn erlang to learn elixir
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New
Allyedge
So, I want to get an Elixir book, but don’t know which one to get. Both Programming Elixir 1.6 and Elixir in Action looks interesting, b...
New
koen_vb
Hi, I was looking for a pointer of how I could most easily start with phoenix regarding deploying it to something like linode or google c...
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement