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 traits #35

Open
asomers opened this issue Jan 5, 2019 · 3 comments
Open

Mock traits #35

asomers opened this issue Jan 5, 2019 · 3 comments

Comments

@asomers
Copy link
Contributor

asomers commented Jan 5, 2019

Mocktopus is very handy for mocking free functions. But when mocking structs, it requires a real copy of the struct to be instantiated. That's not always easy or possible in test environments. That's why most mocking libraries work in terms of traits instead. It would be great if Mocktopus could provide a macro that would instantiate a trait and mock all of its methods. Something like this:

#[mockable]
pub trait A {
    fn foo(&self) -> u32;
}
mock!{AMock, A}

The mock macro could expand to this:

#[derive(Default)]
pub struct AMock {}
#[mockable]
impl AMock {
    fn foo(&self) -> u32 {
        unimplemented!()
    }
}
@CodeSandwich
Copy link
Owner

That's an interesting feature to add in the future. For now what about existing mocking tools? Do they miss anything?

@asomers
Copy link
Contributor Author

asomers commented Mar 18, 2019

No existing mock object library does everything. This is the best comparison of them.
https://asomers.github.io/mock_shootout/

BTW, I'm working on a library of my own, but I haven't released it yet.

@CodeSandwich
Copy link
Owner

That's very interesting, great job with bring it all together and thank you for including Mocktopus!

To be honest I was trying to create a tool for mocking traits too but found obstacles and didn't put enough time into it.

I've dropped idea of building a mock struct based on trait definition passed to a macro very quickly, because it causes a difficult problem when trait inherits from other trait. The mock struct should implement it as well, because it won't compile, but macro has no idea how to do it.

I was trying to construct a fake trait object with https://doc.rust-lang.org/std/raw/struct.TraitObject.html. I think that this approach could give just right amount of power. Unfortunately I couldn't find a way to match fn pointers and trait methods. It's even harder because Rust optimizes away multiple functions delegating to each other and sometimes the same fn pointer shows up in vtable multiple times.

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