asomers / mockall

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

How to do an incremental TDD #444

Closed archietekt closed 1 year ago

archietekt commented 1 year ago

Hello,

First of all, thanks for an amazing mocking library for Rust :slightly_smiling_face: The usage is really simple, docs are nice and it is really powerful, although I am still discovering the range of features available there.

I started learning Rust few weeks ago, so I apologize in advance for any novice issues or non-Rusty thinking :slightly_smiling_face:

I am trying to find a way how to write an incremental tests using the mockall library. For example when I am developing some functionality of specific trait that is using another trait methods inside it, using the TDD, I start with implementing some specific functionality of a method. I setup some expectation that inside that method the method of the other trait is called. Then I implement the functionality to get the test passing. Then I continue with implementing another functionality of the same method. I want to add another call of the trait in that method, so I add another test and some new expectation. But here comes the problem as I always need to setup all expectations that will be called. In this case I will need to modify the previous, already implemented tests which is definitely not the TDD flow how it's supposed to be.

Therefore I would like to know, if there is some possibility how to turn on something like "relaxed" expectations matching - ignore calls that are not explicitely listed in the tests code. For me this was the essential part of the mocking frameworks so far, coming from the C++ (google mock) and C# (moq, ...) environment.

asomers commented 1 year ago

On Mon, Jan 2, 2023, 6:24 AM archietekt @.***> wrote:

Hello,

First of all, thanks for an amazing mocking library for Rust 🙂 The usage is really simple, docs are nice and it is really powerful, although I am still discovering the range of features available there.

I started learning Rust few weeks ago, so I apologize in advance for any novice issues or non-Rusty thinking 🙂

I am trying to find a way how to write an incremental tests using the mockall library. For example when I am developing some functionality of specific trait that is using another trait methods inside it, using the TDD, I start with implementing some specific functionality of a method. I setup some expectation that inside that method the method of the other trait is called. Then I implement the functionality to get the test passing. Then I continue with implementing another functionality of the same method. I want to add another call of the trait in that method, so I add another test and some new expectation. But here comes the problem as I always need to setup all expectations that will be called. In this case I will need to modify the previous, already implemented tests which is definitely not the TDD flow how it's supposed to be.

Therefore I would like to know, if there is some possibility how to turn on something like "relaxed" expectations matching - ignore calls that are not explicitely listed in the tests code. For me this was the essential part of the mocking frameworks so far, coming from the C++ (google mock) and C# (moq, ...) environment.

— Reply to this email directly, view it on GitHub https://github.com/asomers/mockall/issues/444, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA7VOLZ4EDB7PVBS2QDG5LWQLJKNANCNFSM6AAAAAATO2FO3Y . You are receiving this because you are subscribed to this thread.

Well, Mockall certainly can't ignore unexpected method calls. After all, you need to set return values. But if you don't specify any .times(), then any number of calls, including zero, will be accepted .

Message ID: @.***>

asomers commented 1 year ago

Closing this question, on the assumption that the OP found the answer adequate.