How do I create a mock function in Rust? [duplicate]

隐身守侯 提交于 2020-12-13 11:32:11

问题


I'm trying to run unit tests on a function reducer . reducer takes in a struct State and an enum Action and returns a new struct State . When the action is Action::Status , I want the function state.status_function to be called on state. I'm having trouble testing that a mock function I pass into test_status is called with state. Does anyone know what I'm getting wrong in the code below?

fn test_status() {
    let mut status_mock_called: bool;
    let state = State {
        status_function: Box::new(|state: State| {
             status_mock_called = true;
             return state;
        }),
     };
     assert_eq!(root_reducer(state, Action::Status), state);
     assert!(status_mock_called);
}

Outputs the error:

`(dyn std::ops::Fn(State) -> State + 'static)` doesn't implement `std::fmt::Debug`

How can I modify a rust variable from inside a function? Here is the state struct in case it's relevant:

#[derive(Debug, Eq, PartialEq)]
struct State {
    status_function: Box<dyn Fn(State) -> State>,
}

And here is the reducer:

fn root_reducer(state: State, action: Action) -> State {
    match action {
        Action::Status => (state.status_function)(state),
    }
}

来源:https://stackoverflow.com/questions/61941650/how-do-i-create-a-mock-function-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!