2

Given some contract A, that depends on another contract B, what would be the best way to unit test functions from A without inadvertently testing code from B.

Normally, we could use stubs to return data that is the same every time, but this is cumbersome to do manually with even a moderate amount of code (generate, compile, and deploy the stub).

Is there a framework or library out there yet that has this sort of functionality built in?

Also would be happy to provide a code example if the description above is too abstract; just comment.

I'm using:

  • mochajs with chai assertions
  • truffle for a testing framework
  • ethereumjs-testrpc for test network
0

2 Answers 2

1

You can create a stub contract. Deploying it in the test using truffle is not cumbersome. You can do it like so:

token1 = await Token.new()
0

Something I've done in the past is create a separate 'Stub' contract. So if you want to stub contract B, you create another contract BStub that inherits from B and override its methods to return some mock values instead. Then you initialise your A contract with a dependency on BStub instead of B. This has some drawbacks (like unavailability of private functions of B), but it can work well. Not sure if there are better ways to do this using a library or something, but this can be a good workaround.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.