Hi all, I'm learning Rust at this moment and I'm struggling trying to mock the SerialPort trait using serialport and mockall crates:
For example I want to mock the read method:
fn create_mock_port(expected_bytes: Vec<u8>) -> Box<MockSerialPort> {
let mut mock_port = Box::new(MockSerialPort::new());
mock_port.expect_read()
.returning(move |buf| {
buf[..expected_bytes.len()].copy_from_slice(&expected_bytes);
Ok(expected_bytes.len())
});
mock_port
}
And use the mock_port variable for the struct under test:
pub struct SerialPortConnector {
port: SerialPort,
endpoint: SerialPortEndpoint,
}
What is the best approach to obtain my purpose?