audunhalland / unimock

A versatile and developer-friendly trait mocking library
MIT License
71 stars 3 forks source link

Can't mock generic params #14

Closed TheDan64 closed 1 year ago

TheDan64 commented 1 year ago

Saw #7, but doesn't seem related. For example:

#[unimock(api=MockFoo)]
trait Foo {
    fn foo<T>(&self, t: T) -> T;
}
error[E0412]: cannot find type `T` in this scope
  --> src/main.rs:53:25
   |
53 |     fn foo<T>(&self, t: T) -> T;
   |                         ^ not found in this scope

error[E0412]: cannot find type `T` in this scope
  --> src/main.rs:53:31
   |
53 |     fn foo<T>(&self, t: T) -> T;
   |                               ^ not found in this scope

(in a more realistc example, T would be constrained to some trait, like serde::Deserialize)

I would say that this is a pretty big limitation to using entrait/unimock, since you can't mock db queries which specify which data model to deser a query result to

audunhalland commented 1 year ago

Interesting, I guess I forgot to think about this pattern. I believe this shouldn't be too hard to fix, as generic traits are already possible to mock. The MockFn is per-method anyway and it can reuse some of the existing proc-macro code for generics. I'll take a look.

austinjones commented 1 year ago

Hey @audunhalland - I work with @TheDan64 and we are using entrait and unimock for our new API server stack! Just wanted to say thank you for your great work on these libraries. Dependency Injection and mocking in Rust have always been an uphill battle... less so now!

audunhalland commented 1 year ago

0.4.9 has been released with support for this.

TheDan64 commented 1 year ago

Thank you so much!!