Skip to content

Trying to mock std::thread::spawn #455

Answered by asomers
zephod77 asked this question in Questions
Discussion options

You must be logged in to vote

That's because the F parameter is a closure, which is impossible to name. And that makes it impossible to create an expectation for it. You could try this instead:

mock! {
    pub Thread {
        pub fn spawn<T: 'static>(f: Box<FnOnce() -> T> + 'static) -> JoinHandle<T>;
    }
}

then box your closure when you call spawn. But if you really don't want to box closures in production, then you can work around it by defining your mocks like this:

mock! {
    pub Thread {
        pub fn _spawn<T: 'static>(f: Box<FnOnce() -> T> + 'static) -> JoinHandle<T>;
    }
}
impl MockThread {
       pub fn spawn<F, T>(f: F) -> JoinHandle
            where F: FnOnce -> T + Send + 'static,
                  T: 

Replies: 1 comment 21 replies

Comment options

You must be logged in to vote
21 replies
@zephod77
Comment options

@asomers
Comment options

@zephod77
Comment options

@asomers
Comment options

@zephod77
Comment options

Answer selected by zephod77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants