Currently, when writing mocks/tests we need to write some boilerplate code:
#[cfg(test)]
mod color;
#[cfg(not(test))]
mod color_mock;
#[cfg(test)]
mod color {
pub use color_mock::*;
}
Or in this simpler form: Testing and mocking based on name conversion - #3 by zackw
Mock and test name conversion
Here, I propose to add support for mock/test based on name conversion:
mod color;
mod color_mock;
mod color_test;
fn op() {}
fn mock_op {}
fn test_op {}
Comparison with existing solutions:
#[cfg_attr(test, path = "color_mock.rs")]
mod color;
#[cfg(not(test))]
fn op() {}
#[cfg(test)]
fn op() {}
#[cfg(test)]
mod test {
#[test]
fn test_op{}
}
As you can see, the first form looks simpler and easier to remember.
Existing Implementation
There are already some language support this. This means that name conversion is feasible. For example:
Advantages and Disadvantages
Advantages:
- In this way, we can reduce the amount of boilerplate code we write. Then this will reduce the occurrence of some bugs. For example Testing and mocking based on name conversion - #9 by cathaysia
- Reduced mental burden. Newbies can learn to write tests and mock modules in less than a minute.
Disadvantage:
- People who don't understand the naming convention will be confused. This requires programmers to at least read "How to Write Unit Tests". Testing and mocking based on name conversion - #2 by jjpe