This article serves as a record of my recent work. If you notice any errors or have any feedback, please leave a message.
It's important to note that the 3rd party function codes mentioned, such as disk_info
or remaining_time
, are provided as examples and may not guarantee correctness or compilability. These functions are intended to demonstrate certain concepts or serve as starting points for further development. It's essential to review and adapt them according to the specific requirements and environment of your project.
This structure is aims to provide the disk information and the battery information.
There are two crates able to get these information cross platform. sys_info
used to retrieve the disk information and battery
used to retrieve the battery information.
I am unable to isolate the dependencies.
new
functionThe code is work and the 3rd party function calls are isolated! Lets use the mockall
Accroding to the [document|https://docs.rs/mockall/latest/mockall/], I only have to add #[automock]
on the trait right?
No! The function that you gonna mock should have the parameter self
.
Lets implement it.
It seems great right? However, there is some error occur! There is no parameter we can pass into the function when we call it.
What about this pub fn new(self)
? It seems strange to pass self as a parameter to create an instance of itself.
Considering the trait is named "Getter," we can view it as a tool or instrument that allows us to retrieve information. In this case, we can design the function to accept the tong
(the instance implementing the Getter trait) as a parameter. By doing so, the function caller can provide us with the necessary tool to extract the desired information.
Now that we have the tong
, it's important to consider that users may not be aware of the internal structure or requirements of our system. They might not know that they need to pass a tong
to our function.
To address this, we can provide a more user-friendly interface by encapsulating the usage of the tong
within our function. Instead of expecting users to pass a tong
explicitly, we can abstract away the implementation details and provide a simpler and more intuitive API.
By designing our function to hide the complexity and handle the retrieval of information internally, users won't need to explicitly pass a tong
. This approach ensures a smoother and more user-friendly experience, allowing users to interact with our system without needing knowledge of its internal workings.
To effectively test our implementation, we can utilize unit testing and employ mocks to manipulate the return values. It's crucial to note that when writing unit tests for a trait, we should implement the return values for each function defined within the trait.
By creating mock implementations for the trait's functions, we can control and manipulate the behavior of these functions during testing. This enables us to simulate different scenarios and ensure that our code behaves as expected in various conditions.
It's essential to provide thorough test coverage by implementing the necessary return values for each function in the trait. This approach guarantees that our code is rigorously tested and can handle different scenarios and edge cases effectively.