Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Conversation

@kzu
Copy link
Member

@kzu kzu commented Jul 22, 2019

Just like for Arg, Raise and Setup, there is now a Syntax-based Verify which
allows much more succinct code. With the following using at the top:

using static Moq.Syntax;

You can now perform verifications on a mock as follows:

// Verify all "verifiable" calls, i.e. those with a Once/Never/etc. setup.
Verify(calculator);

// Verify a specific invocation was made at least once
Verify(calculator).Add(2, 3);

// Verify it was never called
Verify(calculator).Add(1, 1).Never();

// Verify it was called exactly once
Verify(calculator).Add(1, 1).Once();

Verifiable setups can now just add Once, Never and Exactly directly on
the setup too (unlike v4 which only had Verifiable() which meant AtLeastOnce)

var calculator = Mock.Of<Calculator>();

// The given call is expected only once
calculator.Add(2, 3).Returns(5).Once();

// The given call is never expected
calculator.Add(-1, -1).Never();

// The given call is expected at least once. Equivalent to v4 Verifiable
calculator.Add(1, 1).AtLeastOnce();

// A single call to Verify will verify all the above
Verify(calculator);

// Also supported, non-syntax version
Verify.Called(calculator);
// And instance version (via extension method backwards compatible)
calculator.Verify();

A non-syntax version is also provided, which is basically called from the
syntax one:

// Verify all "verifiable" calls, i.e. those with a Once/Never/etc. setup.
Verify.Called(calculator);

// Verify a specific invocation was made at least once
Verify.Called(calculator).Add(2, 3);

// Verify it was never called
Verify.Called(calculator).Add(1, 1).Never();

// Or alternative syntax depending on style preference
Verify.NotCalled(calculator).Add(2, 3);

A mechanism in the SDK for cloning mocks was added, so that they can be
"frozen" and tweaked independently from the original mock (i.e. for verification
purposes).

As part of cleaning up a mock's behaviors for cloning and verification, we took
the chance to split the former MockTrackingBehavior (which was doing two things)
into MockContextBehavior (the one that sets the MockContext static properties)
and MockRecordingBehavior (which does the actual invocation recording). This
allows getting a cloned mock that does not record calls, for example, for verifying
purposes or diagnostics, say. This could be used to also clone on debugger inspection,
to allow preserving the actual calls instead of having the inspection to modify the
actual calls (which is what happens typically by default in all mocking libraries).

@kzu kzu force-pushed the dev/kzu/verifiy branch from 114018d to 41bd1f2 Compare July 22, 2019 18:55
@stakx
Copy link
Contributor

stakx commented Jul 22, 2019

Verifiable setups can now just add Once, Never and Exactly directly on
the setup too (unlike v4 which only had Verifiable() which meant AtLeastOnce)

Seeing that this makes it into v5, I might actually backport this feature into v4 (in the form of e.g. .Verifiable(times)) as this would solve a long-standing problem people have with verifying setups involving a mutable reference-typed argument (where all but the final state gets lost and can't be verified post-hoc).

@kzu
Copy link
Member Author

kzu commented Jul 22, 2019

Makes sense @stakx! Also, great to see you monitoring this repo! I'll start sending more PRs then 👍

Just like for Arg, Raise and Setup, there is now a Syntax-based Verify which
allows much more succinct code. With the following using at the top:

```
using static Moq.Syntax;
```

You can now perform verifications on a mock as follows:

```
// Verify all "verifiable" calls, i.e. those with a Once/Never/etc. setup.
Verify(calculator);

// Verify a specific invocation was made at least once
Verify(calculator).Add(2, 3);

// Verify it was never called
Verify(calculator).Add(1, 1).Never();

// Verify it was called exactly once
Verify(calculator).Add(1, 1).Once();
```

Verifiable setups can now just add `Once`, `Never` and `Exactly` directly on
the setup too (unlike v4 which only had `Verifiable()` which meant `AtLeastOnce`)

```
var calculator = Mock.Of<Calculator>();

// The given call is expected only once
calculator.Add(2, 3).Returns(5).Once();

// The given call is never expected
calculator.Add(-1, -1).Never();

// The given call is expected at least once. Equivalent to v4 Verifiable
calculator.Add(1, 1).AtLeastOnce();

// A single call to Verify will verify all the above
Verify(calculator);

// Also supported, non-syntax version
Verify.Called(calculator);
// And instance version (via extension method backwards compatible)
calculator.Verify();
```

A non-syntax version is also provided, which is basically called from the
syntax one:

```
// Verify all "verifiable" calls, i.e. those with a Once/Never/etc. setup.
Verify.Called(calculator);

// Verify a specific invocation was made at least once
Verify.Called(calculator).Add(2, 3);

// Verify it was never called
Verify.Called(calculator).Add(1, 1).Never();

// Or alternative syntax depending on style preference
Verify.NotCalled(calculator).Add(2, 3);
```

A mechanism in the SDK for cloning mocks was added, so that they can be
"frozen" and tweaked independently from the original mock (i.e. for verification
purposes).

As part of cleaning up a mock's behaviors for cloning and verification, we took
the chance to split the former `MockTrackingBehavior` (which was doing two things)
into `MockContextBehavior` (the one that sets the `MockContext` static properties)
and `MockRecordingBehavior` (which does the actual invocation recording). This
allows getting a cloned mock that does not record calls, for example, for verifying
purposes or diagnostics, say. This could be used to also clone on debugger inspection,
to allow preserving the actual calls instead of having the inspection to modify the
actual calls (which is what happens typically by default in all mocking libraries).
@kzu kzu force-pushed the dev/kzu/verifiy branch from 41bd1f2 to 414f386 Compare July 22, 2019 22:20
@kzu kzu requested review from adalon and marianor July 22, 2019 22:26
@kzu
Copy link
Member Author

kzu commented Jul 22, 2019

Also, sorry for the massive PR, I iterated quite a bit on this one ¯\(ツ)

@kzu kzu requested a review from stakx July 22, 2019 22:28
@kzu kzu merged commit 6a10327 into master Jul 23, 2019
@kzu kzu deleted the dev/kzu/verifiy branch July 23, 2019 02:57
@stakx
Copy link
Contributor

stakx commented Jul 23, 2019

Also, great to see you monitoring this repo!

I think I really need to take a closer look at the v5 code base soon, so that I can get into v5 mode some more. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants