Serial port mock-up #120
Unanswered
DavidLibault
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Mocking the entire serialport crate indeed sounds like a mighty thing. What test cases do you have in mind? In case that they are mostly about the actual communication, just mocking using the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am trying to make a mock-up to test code using the serialport crate.
From the tester point of view, the mock-up once initialized, is configured for what each mock-up function (
available_ports
,open
,read
andwrite
) returns when it is called. The mock-up will also store the calls and answers of each function, so that the tester code can check which functions are called, in order. This rudimentary tool would be enough to verify code behavior in any situation, and also validate serial protocol implementation.Because
available_ports
andnew
are free floating functions, a global variable is needed to store the responses of this function. Here is how it goes :It works, but has many problems :
1 - The open answer can not contain the SerialPort trait because it doesn't implement sync. Maybe there is a solution with a rust trick.
2 - This doesn't work in multi threaded test (because the global is shared among all threads). There is a crate to handle this issue named "serial_test". Every test using the serial mockup needs to look like this :
It works, but the tests can't be run in parallel anymore...
3 - All the structures, traits, errors etc.. from serialport need to be rewritten, most of them without a change, and the tested code needs to selectively use the mock-up or the real thing :
There must be a better solution...
Is it possible to make such mock-up without requiring a global variable ?
Is it possible to have the mock-up integrated in the module so that when it sees #[cfg(test)] it doesn't implement neither a Posix nor a Windows version but a mock-up version, that with nice rust tricks could be configured with sequential predetermined responses ?
Best regards,
David.
Beta Was this translation helpful? Give feedback.
All reactions