问题
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