Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking external generic struct #347

Closed
MrCasCode opened this issue Nov 4, 2021 · 0 comments
Closed

Mocking external generic struct #347

MrCasCode opened this issue Nov 4, 2021 · 0 comments

Comments

@MrCasCode
Copy link

Hi,

I have this struct in my module

pub struct LiquidityMapBuilder<'a, T: TransportTrait> {
    contract: &'a Contract<T>,
    tick_spacing: i32
}

Contract is a generic struct from an exernal crate web3::contract::Contract

I've tried mocking the struct as:

#[cfg(test)]
trait TransportTrait : Transport + Send + Sync + 'static {}
#[cfg(not(test))]
trait TransportTrait : Transport + Send + Sync {}

#[cfg(test)]
mod mock {
    use super::*;
    use mockall::mock;
    mock! {
        pub Contract <T: TransportTrait>{
            pub fn query<R, A, B, P>(
                &self,
                func: &str,
                params: P,
                from: A,
                options: Options,
                block: B,
            ) -> impl Future<Output = Result<R>> + '_
            where
                R: Detokenize + 'static,
                A: Into<Option<Address>> + 'static,
                B: Into<Option<BlockId>> + 'static,
                P: Tokenize + 'static;
        }
    }
}

#[cfg(test)]
use mock::MockContract as Contract;
#[cfg(not(test))]
use web3::contract::Contract;

From what I've been reading adding the ' static is mandatory for now #217. The problem is that this that now the functions return types don't match because R has a 'static lifetime in the tests. If I don't make it static then I have the problem of not living long enough.

How could I deal with this issue? Being an external crate I can't modify the code to change the return types depending of if testing or not

Repository owner locked and limited conversation to collaborators Nov 4, 2021
@asomers asomers closed this as completed Nov 4, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants