You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First, many thanks for mockall. It's an awesome crate, it has become central to my (and my teams') development experience in Rust.
Not sure if this is possible to do, and I've missed how to do it in the documentation. If a trait has generic parameters, Rust allows a struct (or an enum) to implement multiple times for different parameter objects. How can I mock such an object?
Consider this example:
use mockall::mock;traitMyTrait<T>{fnvalue(&self) -> T;}structMyStruct{integer:i32,float:f32,}implMyTrait<i32>forMyStruct{fnvalue(&self) -> i32{self.integer}}implMyTrait<f32>forMyStruct{fnvalue(&self) -> f32{self.float}}mock!{MyStruct{}implMyTrait<i32>forMyStruct{fnvalue(&self)->i32;}implMyTrait<f32>forMyStruct{fnvalue(&self)->f32;}}
It gives the following error:
error[E0592]: duplicate definitions with name 'expect_value'
This example is derived from my use case: a "Context" object that acts as a centralized container/factory for several entities in my project. It implements a trait of the form Builder<E: Entity> for every of such entities. But I'm assuming there will be much less exotic use cases: think, for example, the From<T> standard trait.
Is there some way to mock an object like this? If there is, could you please point me to it? If not, looks like that an (opt-in) way of manually specifying the name of the "expect_" method would be enough:
Currently there's no way to do it if MyStruct is concrete. If MyStruct is generic, however, then it works and requires no special syntax. Ideally Mockall could create a generic MockMyStruct::expect<T> method, where T is implemented for f32 and i32, but Rust doesn't allow such a thing. For now your workaround is the best possible.
asomers
changed the title
Mock an object that implements the same (generic) trait more than once
Mock the same generic trait twice on a concrete struct
Oct 9, 2021
First, many thanks for mockall. It's an awesome crate, it has become central to my (and my teams') development experience in Rust.
Not sure if this is possible to do, and I've missed how to do it in the documentation. If a trait has generic parameters, Rust allows a struct (or an enum) to implement multiple times for different parameter objects. How can I mock such an object?
Consider this example:
It gives the following error:
error[E0592]: duplicate definitions with name 'expect_value'
This example is derived from my use case: a "Context" object that acts as a centralized container/factory for several entities in my project. It implements a trait of the form
Builder<E: Entity>
for every of such entities. But I'm assuming there will be much less exotic use cases: think, for example, theFrom<T>
standard trait.Is there some way to mock an object like this? If there is, could you please point me to it? If not, looks like that an (opt-in) way of manually specifying the name of the "expect_" method would be enough:
The text was updated successfully, but these errors were encountered: