Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock struct in external crate does not work #26

Closed
kungfucop opened this issue Mar 12, 2020 · 3 comments
Closed

Mock struct in external crate does not work #26

kungfucop opened this issue Mar 12, 2020 · 3 comments

Comments

@kungfucop
Copy link

kungfucop commented Mar 12, 2020

// crate1
#[cfg_attr(test, faux::create)]
pub struct SettingApi;

#[cfg_attr(test, faux::methods)]
impl SettingApi {
    pub fn get_settings(){}
}

//crate2
mod tests {
    use crate1::SettingApi
    #[test]
    fn testReal() {
        let mut api = SettingApi::faux();
        faux::when!(api.get_settings).safe_then(|req_cmd| {
             // print something
        });
    }
}
Error error[E0599]: no function or associated item named `faux` found for type `SettingApi` in the current scope
@nrxus
Copy link
Owner

nrxus commented Mar 12, 2020

I took the liberty of editing the formatting of your post a bit, I hope you do not mind it was just a bit hard to read.

Unfortunately, this is the expected behavior due to the way #[cfg(test)] works in Rust. When a different crate is pulled over, even if you are running tests, it will not build that dependent crate with the test cfg enabled for them, it will only enable the test cfg within the crate currently being tested.

For more info: https://stackoverflow.com/questions/41700543/can-we-share-test-utilites-between-crates.

Eventually I want to provide workarounds for this limitation, or at least specified suggestions within a small guide of how to make it work but it couldn't be through the #[cfg(test] unfortunately.

@kungfucop
Copy link
Author

Thank you @nrxus for the answer. Please keep the good work going, I search all github trying to find something could mock my db/network calls easily. Feel there is a lot potential in faux!

A guide on how to work around this for now will be super helpful!!

@nrxus
Copy link
Owner

nrxus commented Mar 13, 2020

I also feel like there is a lot of potential here, thanks for looking at faux!

I created a new issue specifically about exploring and documenting a way to export mocks through crates so I am going to close this one.

@nrxus nrxus closed this as completed Mar 13, 2020
timabell added a commit to rustworkshop/gitopolis that referenced this issue Nov 16, 2022
Doesn't work because src/ not compiled with cfg(test) switch by design
in rust when building integration tests in tests/

nrxus/faux#26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants