O'Reilly logo

Metaprogramming Elixir by Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Smarter Testing with Macros

If you’re familiar with writing tests in most mainstream languages, you know it can take a while to learn the different assertion functions of testing frameworks. For example, let’s see how a few basic assertions for popular test frameworks in Ruby and JavaScript compare to Elixir. You don’t need to be familiar with these languages; just be mindful of the different assertion APIs.

JavaScript:

 
expect(value).toBe(true);
 
expect(value).toEqual(12);
 
expect(value).toBeGreaterThan(100);

Ruby:

 
assert value
 
assert_equal value, 12
 
assert_operator value, :<=, 100

Elixir:

 
assert value
 
assert value == 12
 
assert value <= 100

Notice how such simple assertions took on arbitrary method and function names in Ruby and JavaScript? ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required