Test private instance methods


#1

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.


#2

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


#3

Thanks :slight_smile:


#4

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.