asomers / mockall

A powerful mock object library for Rust
Apache License 2.0
1.5k stars 62 forks source link

Feature Request: Mock object IDs #524

Open chanq-io opened 10 months ago

chanq-io commented 10 months ago

Hello!

Firstly, respect due on this amazing crate. Has made testing a dream for projects I've been working on.

I was wondering if there would be anyway to construct Mocks with some kind of identifier? Or the some way to add contextual information to expectations? I realise anything with a member variables is typically un-mock-like, but for larger integration tests with tons of mocks flying around I often am left scratching my head when something falls over. For example, say I have 20-30 MockThing instances wrapped in MyContainer which performs a series of operations on them by calling .foo(x, y) and each MockThing::foo has custom expectations defined on it... When one of those expectations fails, I get a panic like:

thread 'tests::my_container' panicked at 'MockThing::foo(?, ?): No matching expectation found'

Which is a pain to diagnose.. But, if we had some contextual information added to the Mock or Expectation, then it would make it much easier to fix.

Thanks

asomers commented 10 months ago

I'm glad you like it! As for your question, one thing immediately comes to mind: you'll get better error messages if you enable the nightly crate feature. As its name suggests, that requires a nightly compiler. Or, if you use the latest unreleased crate from the git master branch, you'll also get better error messages even without the nightly feature.

As for some kind of mock id, I can see how that would be useful for your case. Other than panic messages, are there any other places where you think it might be helpful?

chanq-io commented 10 months ago

Thanks for the quick response.

nightly is only an option for my personal projects unfortunately, work requirements are stricter. But, good to know I can pull tip of main to get better error messages.

In addition to improved panics, it could be useful to have a derived Display implementation that output this user defined contextual info so you could print whole test objects prior to running whatever logic causes a panic