1

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),
    }
}
fifn2
  • 382
  • 1
  • 6
  • 15
  • 4
    Use a [closure](https://doc.rust-lang.org/book/ch13-01-closures.html) instead. – Robin Zigmond May 21 '20 at 18:48
  • 3
    It's not clear what you're trying to achieve. Can you edit your question and clarify what you'd like your function named `a` to actually do? Then we can describe a suitable way to do it using Rust. – Bobulous May 21 '20 at 18:50
  • @Bobulous So basically this was a simplified version of my way more complex question, so I will try the more complex version and see if anyone understands – fifn2 May 21 '20 at 20:29

0 Answers0