The embedded_hal_mock::eh1::digital module provides mock implementations for InputPin, OutputPin, StatefulOutputPin, and (if embedded-hal-async is enabled) the Wait trait.
To use these mocks, you define a sequence of Transaction objects representing the expected interactions with the pin. These expectations are passed to Mock::new(&expectations). The mock will verify that every call made to the pin matches the expected transaction in order. If a call does not match or if there are remaining expectations after the test, the mock will panic or fail validation.
Key lifecycle methods:
pin.done(): Validates that all expected transactions have been consumed.
use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin, ErrorKind};
use embedded_hal_mock::eh1::digital::{Mock as PinMock, State as PinState, Transaction as PinTransaction};
let err = ErrorKind::Other;
// Configure expectations
let expectations = [
PinTransaction::get(PinState::High),
PinTransaction::get(PinState::High),
PinTransaction::set(PinState::Low),
PinTransaction::set(PinState::High).with_error(err.clone()),
PinTransaction::get_state(PinState::High),
PinTransaction::toggle(),
PinTransaction::get_state(PinState::Low),
];
// Create pin
let mut pin = PinMock::new(&expectations);
// Run and test
assert_eq!(pin.is_high().unwrap(), true);
assert_eq!(pin.is_low().unwrap(), false);
pin.set_low().unwrap();
pin.set_high().expect_err("expected error return");
pin.is_set_high().unwrap();
pin.toggle().unwrap();
pin.is_set_low().unwrap();
pin.done();
// Update expectations
pin.update_expectations(&[]);
pin.done();