Test private instance methods

Is there a way to test private instance methods?

pub struct Bar {
    // some fields
}

impl Bar {
    fn f3() {}
    pub fn f4() {}
}

mod tests {
    fn f3_testing() {
        let bar = Bar {};
        bar.f3() // this doesn't work
    }

    fn f4_testing() {
        let bar = Bar {};
        bar.f4(); // this works
}

It would be ideal if it would be possible to test private instance methods the same way that you can test private static methods.

Your code is referencing a Foo that doesn't exist; Should Bar be Foo?

1 Like

Thanks :slight_smile:

Works for meTM

I had to add the &self parameters to the functions to get the tests to compile, so I also added versions without them to make sure it's not something to do with inherent class methods vs inherent instance methods.

Generally any submodule should be able to access all the private details from its parent modules, it's only parents and siblings that aren't allowed to peek.

2 Likes