asomers / mockall

A powerful mock object library for Rust
Apache License 2.0
1.5k stars 62 forks source link

Support setting an automock as the associated type in another mock? #505

Closed bbaldino closed 1 year ago

bbaldino commented 1 year ago

I've got a trait with a function that returns another trait (represented via an associated type), that I'd like to be able to return a mock of, in the automock case. For example:

#[automock]
trait KeyValueStore { ... }

// Here I need to set a default value for the associated type...since I'm using this for testing, I want it to return the mocked version of KeyValueStore
#[automock(type KeyValueType=MockKeyValueStore)]
trait StorageManager {
    type KeyValueType: KeyValueStore;

    ...    
}

Is it possible to do this? Or is there a better way to accomplish what I'm trying to do here?

asomers commented 1 year ago

Yeah, that should work fine. Have you tried it yet?

bbaldino commented 1 year ago

Yes, sorry--I continued playing with things a bit more and believe I have something working now...I was waiting to verify a bit more before closing this.

From what I can tell so far, the key part that I seemed to be missing was fully-qualifying the name there (i.e. crate::kv_store::MockKeyValueStore). I'll keep validating this and close this out later today if everything is looking good. Thanks!

bbaldino commented 1 year ago

All good, sorry for the noise 👍