Skip to content

Feature Request: use real implementation in some struct methods #675

@alordash

Description

@alordash

Hi!
First of all - thank you for your crate, really helpful and easy to use.

I want the ability to write tests like this:

struct Foo;
impl Foo {
    pub fn work(&self) { self.extra_work(); }
    fn extra_work(&self) {}
}

#[cfg(test)]
mod tests {
    fn work_calls_extra_work() {
        // Arrange
        let foo = FooMock::new();

        // Act
        foo.work();

        // Assert
        foo.expect_extra_work.times(1);
    }
}

However this will not currently work because foo.work() would call mock's implementation that does nothing. To fix it I imagine we'd need something like this:

fn work_calls_extra_work() {
    // Arrange
    let foo = FooMock::new();
    foo.work().call_impl(); // tell mockall that this method must call real method's implementation

    // Act
    foo.work();

    // Assert
    foo.expect_extra_work.times(1);
}

Is it possible to add something like that? I would gladly help to implement it if you think that it can be done.

I imagine it would require storing _impl: Option<Foo> in FooMock and also adding some way to pass Foos constructor arguments to FooMock::new().

This is analog to CallBase() method from C#'s mocking framework Moq. (https://stackoverflow.com/a/31968342)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions