galvanic-mock icon indicating copy to clipboard operation
galvanic-mock copied to clipboard

Can't mock methods with generic parameters

Open asomers opened this issue 6 years ago • 0 comments

It seems that galvanic-mock can mock a method with generic parameters, but only if the match and return expressions are written purely in terms of the generic types. That's not very useful. What users really need is to be able to mock a generic method for a particular choice of parameter. And in some cases, it's necessary to mock the same method more than once for different parameter choices, all in the same test.

Here's an example demonstrating the problem with match and return statements:

    fn generic_parameters() {
        let mock = new_mock!(GenericMethodTrait);
        given! {
            <mock as GenericMethodTrait>::foo(|x| *x == 5u32) then_return 100u32 always;
        }
        assert_eq!(100, mock.foo(5));
    }

This will result in the following compiler errors:

error[E0282]: type annotations needed
  --> src/t_galvanic_mock.rs:93:1
   |
93 | #[use_mocks]
   | ^^^^^^^^^^^^ consider giving this closure parameter a type
   |
   = note: type must be known at this point

error[E0308]: mismatched types
  --> src/t_galvanic_mock.rs:93:1
   |
93 | #[use_mocks]
   | ^^^^^^^^^^^^ expected type parameter, found u32
   |
   = note: expected type `T`
              found type `u32`

asomers avatar Jan 13 '19 15:01 asomers