Seeking Example of mocking input for button long press. #108
Replies: 3 comments 5 replies
-
I think you might be conflating 2 things which are independent of each other: One, the mocking and testing of For For
This requires you to write your code as C++ templates, and inject a
This assumes that you are using classes and OOP techniques. You create a virtual method named The advantage of this technique is that it's usually easier to understand for most people. The disadvantage is that the runtime polymorphism of the See
Instead of using the Arduino The advantage of this is that the code is quite simple and easy to understand. The disadvange is that you have to have modify the calling code to use your custom clock functions, instead of the Arduino builtin clock functions. The other disadvantage is that there is a runtime overhead from the code checks whether the code running in test mode or production mode, similar to the overhead of the virtual function
It might be possible to use the C/C++ preprocessor to link in a different implementation of unsigned long myCustomMillis() {
#if TESTING_MODE
...
#else
...
#endif
} The problem with this technique is that it requires the ability to define the Hope this gives you enough to point you in the right direction. |
Beta Was this translation helpful? Give feedback.
-
I ended up implementing the suggestions and they are working for me. In my main |
Beta Was this translation helpful? Give feedback.
-
(Reopening. I prefer to keep Discussions open so that other people can search for them easier. Issues are closed when they are fixed.) |
Beta Was this translation helpful? Give feedback.
-
I've written a function for detecting user input from a physical button. It uses a while loop that begins with a
digitalRead
and callsmillis()
to determine when the button is pressed and let go. The length of time leads to whether this was a short press or a long press.I've seen AUnit examples that mock different values that
digitalRead
will return for a particular pin, but I'm wondering about how to tie this to time. Something like "at this time, return this mock value, and then at this later time, change the value."If it's not possible to accomplish this mocking with the
digitalRead
, I can always wrap my button class or inherit it from an abstract class (an interface) and build a mock class to use in the tests - but if I can mock the time and change in values from the read, that helps prove more of my code is sound.Does anyone have examples I can reference?
Beta Was this translation helpful? Give feedback.
All reactions