ESpec Composable Matchers

I wasn’t sure where to put this one :frowning:

I am using ESpec for my project as I am a ruby dev coming across to elixir and it felt quite natural to me.
I noticed that there are no examples for composable matchers like in rspec and I was wondering if there is an alternative way of doing it either via elixir itself or some undocumented feature of espec.

Here is an example from rspec

expect(my_array).to contain_exactly(a_hash_including(test: 1), a_hash_including(test: 2))

So in this case contain_exactly has other matchers as its value


Maybe this is effectively off topic, but I don’t find that any more readable than.

assert [%{test: 1}, %{test: 2}] = my_array

The function based approach used in ruby is necessary because it doesn’t have macro based meta-programming and thus if you assert foo == bar in ruby all that assert knows is that it got false when it expected true, which makes for a crappy error message. The assert macro in Elixir though is smarter than that, and it seems to just obviate the need for this stuff.


Youre probably right. Ive not been doing elixir long so have not harnessed the power of pattern matching in tests yet.


1 Like