New Waffle matcher: expect().to.be.calledOnContract()

Marek Kirejczyk
Jun 3, 2020 · 3 min read

Perhaps you read my previous post about mocking smart contracts. Now, if you did, you may probably agree that writing tests is still a challange. One of the fundamental reasons is that we approach them as if they were integration tests rather than unit tests.

This time, we are going to focus on another aspect of the same challenge: testing the effects of a call.

Testing effects

Let’s examine once more a testing scenario for a hypothetical ERC721 market that allows trading non-fungible tokens for ERC20 tokens.

Once a call to the contract under test (Market on our diagram) is made, one needs to inspect the effects of the call. We usually do that by querying the state of the related smart contracts. This might be somewhat complex at times, as we need to look into the state of different smart contracts, which in some cases might be private. In other cases, the state might poorly reflect interactions between contracts.

Wouldn’t it be useful if we could examine precisely what the interactions between a contract under test and its dependencies are?

expect(…).to.be.calledOnContract(…);

For those complex cases, we’ve introduced a new matcher that allows us to specify expectations as to which function should be called, on which contracts, and with which arguments.

Let’s examine an example smart contract.

Now, with the new matcher, we can test it in the following ways. Firstly, examine if a specific function on the contract was called:

Secondly, specify exact parameters to be used in the call:

Full example available here.

Works with Mock and… any contract

And here is the best part. The new matchers work with both normal and mock contracts described in the previous post. It is because Waffle records and filters EVM calls rather than inject code, like it is the case of popular testing libraries for other technologies.

Important notes

This feature was introduced in Waffle version 2.5.

Currently, it doesn’t work with Buidler which some of you are using together Waffle. However, we’ll be working with the Nomic Labs team in the following weeks to integrate the matchers into the framework.

We are Ethworks. A truly remarkable team for your blockchain project.

Find us on Twitter, Dribbble and GitHub.

Ethworks

On the blockchain, software engineering and running a company.

Sign up for 💡 Waffle News 💡

By Ethworks

Best practices in testing smart contracts with Waffle: our sweeter and simpler library built for Ethereum developers Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Ethworks

Software is eating the world. Blockchain is eating money. Ethworks is cooking.

Marek Kirejczyk

Written by

Ethereum blockchain Engineer. Ethworks, Universal Login.

Ethworks

Software is eating the world. Blockchain is eating money. Ethworks is cooking.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store