Section
Getting Started
API
Features
Guides
Reference
AI Access
Testing without hardware
Assert the frames with MockBoxWith the mock feature, drive a Device with a MockBox and assert the queued frames through recorded_frames.
cargo add medius --features mockEXAMPLE
use medius::{Button, Device, FrameType, MockBox};
#[test]
fn press_queues_an_inject() {
let mock = MockBox::new();
let device = Device::with_mock(mock.clone());
device.press(Button::Left).unwrap();
assert!(mock.saw(FrameType::Inject));
let frame = mock
.recorded_frames()
.into_iter()
.find(|f| f.ty == FrameType::Inject)
.unwrap();
assert_eq!(frame.payload, vec![0, 0, 0, 1]);
}Driving logs in a test
Push log lines with a MockBoxpush_log on a MockBox emits a LOG frame that surfaces on logs() like a real one.
EXAMPLE
use medius::{Device, LogLevel, MockBox};
use std::time::Duration;
#[test]
fn logs_reach_the_stream() {
let mock = MockBox::new();
let device = Device::with_mock(mock.clone());
let stream = device.logs();
mock.push_log(LogLevel::Warn, "overheating");
let line = stream.recv_timeout(Duration::from_secs(1)).unwrap();
assert_eq!(line.level, LogLevel::Warn);
assert_eq!(line.text, "overheating");
}Testing async code
A MockBox behind an AsyncDeviceThere's no AsyncDevice::with_mock: build a mocked Device, then call into_async. Drive the futures with block_on, so the test needs no async runtime. A silent box never answers, resolving the query to Err(Error::QueryTimeout).
EXAMPLE
use futures::executor::block_on;
use medius::{Device, Error, MockBox, Version};
let mock = MockBox::new().with_version(Version {
proto_ver: 3, fw_major: 1, fw_minor: 2, fw_patch: 3, mac: [0; 6], name: "Loki".into(),
});
let device = Device::with_mock(mock).into_async();
let v = block_on(device.query_version())?;
assert_eq!((v.fw_major, v.fw_minor, v.fw_patch), (1, 2, 3));
// a silent box times out:
let device = Device::with_mock(MockBox::new().silent()).into_async();
assert!(matches!(block_on(device.query_version()).unwrap_err(), Error::QueryTimeout));