Your questiones are very good. I will try to answer your question here.
For testing, what is the file's position in the module hierarchy?
I think xxx_test.rs has the same hierarchy with xxx.rs.
Can xxx_test.rs
access non-pub
members of xxx.rs
?
No, it cannot, xxx_test.rs can only test pub members of xxx.rs.1
What file is considered to have its mod
statement.
The file contains xxx.rs
will contain xxx_test.rs
(compiler will insert mod xxx_test implicit. Yes, this is actually a sugar). And if xxx.rs be masked by cfg
, then xxx_test.rs will be masked also.
For mocks, what makes the mock "active"?
Here, mock is considered as a tool to assist unit testing. So cfg(test)
will active xxx_mock.rs
If I have xxx_mock.rs
and xxx_test.rs
, does xxx_test
get access to xxx
or xxx_mock
APIs?
If you have both xxx_test.rs and xxx_mock.rs, then you cannot access xxx.rs's methods. As mentioned above, mock is considered as a tool to assist unit testing. If you only need mock a subset of functions of a module, a better approach is to extract it into a submodule. Then create submodule_mock.rs.
Also some way to "compare" APIs in the "real" and "mock" modules and diagnose differences.
I think the purpose of mock is to build a simulated environment for unit testing, such as returning some pre-prepared data when calling IO interface. More complex situations should be handled by integration testing. After all, unit testing only tests units. We should carefully handle the boundary between unit testing and integration testing.
I, personally, also really don't find "but lang X does it" all that convincing either for that matter.
Yes. I mention these points solely to provide empirical evidence that grounds my argument, ensuring it is not mere speculation.
When you look at my answers, you'll see that I add some references. Which are to provide some factual basis. They are not means "XXX did this, so we should do this too"