mockall icon indicating copy to clipboard operation
mockall copied to clipboard

Make some of internal API public so that other crates can build on top of mockall

Open taminomara opened this issue 3 years ago • 8 comments

TLDR: I'm writing a crate that will allow mocking ethereum smart contracts, and I want it to work nicely with mockall. For that, I'd like to request making SeqHandle a part of official public API.

Longer explanation.

So, ethereum contracts are like structs (with data and methods) that live in a blockchain. I'm writing a crate that will allow mocking them without using an actual chain. The idea is that you create a mock blockchain and deploy mock contracts onto it. When you deploy a contract, you get an object for setting up expectations. Since we use mockall quite extensively in our projects, I've decided to mirror its API in this object:

let chain = mock_chain();

let contract = chain.deploy_contract(...);
contract.expect_call("MethodA")
    .once()
    .with(...)
    .returning(...);

Now I want to implement in_sequence, and I want it to work with mockall's native SeqHandle. This will allow creating sequences that combine calls to contracts and to normal mocked traits and functions.

taminomara avatar Jul 26 '21 08:07 taminomara

Can you give an example of how you would use SeqHandle?

asomers avatar Jul 26 '21 12:07 asomers

I suppose I'd use it the same way it is used by the generated code. I.e., my struct that represents mocked contract will have in_sequence method, which takes a Sequence, generates a SeqHandle and stores it. Then, whenever and expectation is satisfied or verified, I'll call satisfy or verify method on the stored handle.

taminomara avatar Jul 26 '21 12:07 taminomara

To help me understand, could you provide a code sample, even one that doesn't compile?

asomers avatar Jul 26 '21 13:07 asomers

Sorry, was away for some time.

Here's an example of our crate's API when used with sequences: https://github.com/gnosis/ethcontract-rs/blob/main/ethcontract-mock/src/test/mod.rs#L118

Specifically, we save mockall's sequence in our code, then we verify and satisfy it when contract methods are called.

taminomara avatar Aug 16 '21 08:08 taminomara

It looks like you've reimplemented almost all of Mockall, keeping only Sequence, one of the smallest components. Correct? If so, I don't think that's sufficient reason to publicize Sequence's internal API. If I do that, then I would be committed to keeping it stable.

asomers avatar Aug 16 '21 14:08 asomers

I mean, not exactly. I've manually implemented the same code for smart contracts that mockall would generate for traits and structs. Thus, it uses this semi-public API for generated code. And I'd like it to be interoperable with mockall's Sequence. Otherwise, I'll have to roll my own Sequence and loose the entire point of re-implementing mockall's interface.

taminomara avatar Aug 17 '21 04:08 taminomara

What is the point of reimplementing all of Mockall's generated code, anyway? Are you trying to mix calls to the same Sequence objects from both generated code and your own custom code?

asomers avatar Aug 17 '21 12:08 asomers

Yes, that's exactly what I'm doing.

taminomara avatar Aug 17 '21 12:08 taminomara