Giter Site home page Giter Site logo

Comments (6)

asomers avatar asomers commented on August 23, 2024

Could you please post a complete example?

from mockall.

dr-kernel avatar dr-kernel commented on August 23, 2024

A more complete example of how the create i'm attempting to mock is used or how i would use it in a test? Or both?

from mockall.

asomers avatar asomers commented on August 23, 2024

A minimal example that demonstrates your problem. The one you posted above is incomplete.

from mockall.

dr-kernel avatar dr-kernel commented on August 23, 2024

Please let me know if any further clarification is required. Thank you!

use std::io::Error;
use tokio_modbus::prelude::*;
use mockall::*;
use async_trait::async_trait;
mock! {
    #[allow(dead_code)]
    #[derive(Debug)]
    Context {}

    #[async_trait]
    impl Client for Context {
        async fn call(&mut self, request: Request<'_>) -> Result<Response, std::io::Error> {}
    }
    impl SlaveContext for Context {
        fn set_slave(&mut self, slave: Slave) {}
    }
}
fn main() -> Result<(), Error> {
    let mock = MockContext::new();
    let v = vec![1234u16];
    mock.returning(v);
    let reg = mock.read_input_registers(1u16, 1);
    assert_eq!(reg, v);
    Ok(())
}

from mockall.

asomers avatar asomers commented on August 23, 2024

So Mockall can't yet handle the anonymous lifetime in an argument position. It could with some extra work. But it usually doesn't need to, because when using mock! the user always has the option to give the lifetime a name. In this case, it's hard for you to name the lifetime, because async_trait adds some extra lifetimes. So in order to make this work, you have to use cargo expand to see what async_trait is actually doing. But once you've done that, you can easily make it work, like this:

use async_trait::async_trait;
use mockall::*;
use std::io::Error;
use tokio_modbus::prelude::*;

use core::future::Future;
use std::pin::Pin;

mock! {
    #[allow(dead_code)]
    #[derive(Debug)]
    Context {}

    #[async_trait]
    impl Client for Context {
        fn call<'life0, 'life1, 'async_trait>(
            &'life0 mut self,
            request: Request<'life1>,
        ) -> Pin<
                Box<dyn Future<Output = Result<Response, Error>> + Send + 'async_trait>
            >
        where
            'life0: 'async_trait,
            'life1: 'async_trait,
            Self: 'async_trait,
        {}
    }
    impl SlaveContext for Context {
        fn set_slave(&mut self, slave: Slave) {}
    }
}

from mockall.

dr-kernel avatar dr-kernel commented on August 23, 2024

Thank you for that explanation. I'll look into cargo expand, I saw it in the playground but hadn't installed the tool locally. Appreciate the your knowledge. That does assist the compiler. I'll keep this in mind for the future!

from mockall.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.