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

Add setup.Verifiable(Times times, [string failMessage]) method #1319

Merged
merged 3 commits into from
Dec 31, 2022

Conversation

stakx
Copy link
Contributor

@stakx stakx commented Dec 30, 2022

Better late than never. 😁

Usage summary:

1. Specifying a maximum number of allowed calls:

var bomb = new Mock<IBomb>();
bomb.Setup(b => b.Poke()).Verifiable(Times.AtMostOnce);
bomb.Object.Poke();
bomb.Object.Poke();  // throws `MockException` right away, without an explicit `mock.Verify()`

(This is mostly equivalent to the deprecated setup.AtMostOnce() and setup.AtMost(n) methods, except those didn't mark a setup as verifiable.)

2. Using mock.Verify[All]() to check whether the setup was called the correct number of times:

var bomb = new Mock<IBomb>();
bomb.Setup(b => b.Poke()).Verifiable(Times.Never);
bomb.Verify();  // passes

3. Excluding a setup from mock.VerifyAll() using (e.g.) Times.AtMost(int.MaxValue):

var anyNumberOfTimes = Times.AtMost(int.MaxValue);
var bomb = new Mock<IBomb>();
bomb.Setup(b => b.Poke()).Verifiable(anyNumberOfTimes);
bomb.Object.Poke();
bomb.VerifyAll();  // passes, even if the above line of code were removed

(It would be nicer to have something like Times.Any, however that wouldn't make sense in most scenarios and might confuse users into doing strange things, so I chose not to add that to the library for now.)

4. Verifying the number of calls in situations where invocation arguments are modified after the invocation (but before verification) happened:

 var product = new Product();
 var inventory = new Mock<IInventory>();
+inventory.Setup(a => a.Add(It.Is<Product>(p => p.Type == "Flower"))).Verifiable(Times.Once);

 product.Type = "Flower";
 inventory.Object.Add(product);
 product.Type = "Bomb";

-inventory.Verify(i => i.Add(It.Is<Product>(p => p.Type == "Flower")), Times.Once);  // fails because product is now a bomb
+inventory.Verify();  // works because the setup triggered when product was still a flower

Closes #373, closes #937, closes #1210.

@stakx stakx added this to the 4.19.0 milestone Dec 30, 2022
@stakx stakx self-assigned this Dec 30, 2022
@stakx stakx changed the title Add setup.Verifiable(Times times, [string failMessage]) method Add setup.Verifiable(Times times, [string failMessage]) method Dec 30, 2022
@stakx stakx force-pushed the verifiable-times branch 2 times, most recently from 50d2c4f to c75cb6b Compare December 31, 2022 11:44
@rishisolutionsbysms
Copy link

When this is going to released?

@stakx
Copy link
Contributor Author

stakx commented Apr 30, 2023

I'd love to do a release but I'm currently blocked from doing so by #1340.

@andy1547
Copy link

andy1547 commented Aug 1, 2023

This feature has been monumental in allowing us to migrate from NMock to Moq without rewriting 3000+ tests to the AAA style. @stakx We'd be incredibly grateful if you could create a new release :')

vbreuss referenced this pull request in Testably/Testably.Architecture.Rules Aug 8, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [Moq](https://togithub.com/moq/moq) | nuget | minor | `4.18.4` ->
`4.20.0` |

---

### Release Notes

<details>
<summary>moq/moq (Moq)</summary>

### [`v4.20.0`](https://togithub.com/moq/moq/releases/tag/v4.20.0)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### ✨ Implemented enhancements

- Add `setup.Verifiable(Times times, [string failMessage])` method by
[@&#8203;stakx](https://togithub.com/stakx) in
[https://github.com/moq/moq/pull/1319](https://togithub.com/moq/moq/pull/1319)

##### 🔨 Other

- Add `Mock<T>.RaiseAsync` by
[@&#8203;stakx](https://togithub.com/stakx) in
[https://github.com/moq/moq/pull/1313](https://togithub.com/moq/moq/pull/1313)
- Add `ThrowsAsync` for non-generic `ValueTask` by
[@&#8203;johnthcall](https://togithub.com/johnthcall) in
[https://github.com/moq/moq/pull/1235](https://togithub.com/moq/moq/pull/1235)
- Use PackageLicenseExpression instead of PackageLicenseUrl by
[@&#8203;wismann](https://togithub.com/wismann) in
[https://github.com/moq/moq/pull/1322](https://togithub.com/moq/moq/pull/1322)
- Don't throw away generic type arguments in one
`mock.Protected().Verify<T>()` method overload by
[@&#8203;stakx](https://togithub.com/stakx) in
[https://github.com/moq/moq/pull/1325](https://togithub.com/moq/moq/pull/1325)
- [#&#8203;1340](https://togithub.com/moq/moq/issues/1340) updated
appveyor.yml with workaround to make builds work again by
[@&#8203;david-kalbermatten](https://togithub.com/david-kalbermatten) in
[https://github.com/moq/moq/pull/1346](https://togithub.com/moq/moq/pull/1346)
- Revamp structure, apply oss template, cleanup projects/imports by
[@&#8203;kzu](https://togithub.com/kzu) in
[https://github.com/moq/moq/pull/1358](https://togithub.com/moq/moq/pull/1358)
- Add 💜 SponsorLink support by [@&#8203;kzu](https://togithub.com/kzu)
in
[https://github.com/moq/moq/pull/1363](https://togithub.com/moq/moq/pull/1363)
- fix website url by [@&#8203;tibel](https://togithub.com/tibel) in
[https://github.com/moq/moq/pull/1364](https://togithub.com/moq/moq/pull/1364)

#### New Contributors

- [@&#8203;johnthcall](https://togithub.com/johnthcall) made their first
contribution in
[https://github.com/moq/moq/pull/1235](https://togithub.com/moq/moq/pull/1235)
- [@&#8203;wismann](https://togithub.com/wismann) made their first
contribution in
[https://github.com/moq/moq/pull/1322](https://togithub.com/moq/moq/pull/1322)
- [@&#8203;david-kalbermatten](https://togithub.com/david-kalbermatten)
made their first contribution in
[https://github.com/moq/moq/pull/1346](https://togithub.com/moq/moq/pull/1346)
- [@&#8203;dependabot](https://togithub.com/dependabot) made their first
contribution in
[https://github.com/moq/moq/pull/1360](https://togithub.com/moq/moq/pull/1360)

**Full Changelog**: moq/moq.spikes@v4.18.4...v4.20.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/Testably/Testably.Architecture.Rules).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNy4xIiwidXBkYXRlZEluVmVyIjoiMzYuMjcuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
vbreuss referenced this pull request in Testably/Testably.Abstractions Aug 8, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [Moq](https://togithub.com/moq/moq) | nuget | minor | `4.18.4` ->
`4.20.0` |

---

### Release Notes

<details>
<summary>moq/moq (Moq)</summary>

### [`v4.20.0`](https://togithub.com/moq/moq/releases/tag/v4.20.0)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### ✨ Implemented enhancements

- Add `setup.Verifiable(Times times, [string failMessage])` method by
[@&#8203;stakx](https://togithub.com/stakx) in
[https://github.com/moq/moq/pull/1319](https://togithub.com/moq/moq/pull/1319)

##### 🔨 Other

- Add `Mock<T>.RaiseAsync` by
[@&#8203;stakx](https://togithub.com/stakx) in
[https://github.com/moq/moq/pull/1313](https://togithub.com/moq/moq/pull/1313)
- Add `ThrowsAsync` for non-generic `ValueTask` by
[@&#8203;johnthcall](https://togithub.com/johnthcall) in
[https://github.com/moq/moq/pull/1235](https://togithub.com/moq/moq/pull/1235)
- Use PackageLicenseExpression instead of PackageLicenseUrl by
[@&#8203;wismann](https://togithub.com/wismann) in
[https://github.com/moq/moq/pull/1322](https://togithub.com/moq/moq/pull/1322)
- Don't throw away generic type arguments in one
`mock.Protected().Verify<T>()` method overload by
[@&#8203;stakx](https://togithub.com/stakx) in
[https://github.com/moq/moq/pull/1325](https://togithub.com/moq/moq/pull/1325)
- [#&#8203;1340](https://togithub.com/moq/moq/issues/1340) updated
appveyor.yml with workaround to make builds work again by
[@&#8203;david-kalbermatten](https://togithub.com/david-kalbermatten) in
[https://github.com/moq/moq/pull/1346](https://togithub.com/moq/moq/pull/1346)
- Revamp structure, apply oss template, cleanup projects/imports by
[@&#8203;kzu](https://togithub.com/kzu) in
[https://github.com/moq/moq/pull/1358](https://togithub.com/moq/moq/pull/1358)
- Add 💜 SponsorLink support by [@&#8203;kzu](https://togithub.com/kzu)
in
[https://github.com/moq/moq/pull/1363](https://togithub.com/moq/moq/pull/1363)
- fix website url by [@&#8203;tibel](https://togithub.com/tibel) in
[https://github.com/moq/moq/pull/1364](https://togithub.com/moq/moq/pull/1364)

#### New Contributors

- [@&#8203;johnthcall](https://togithub.com/johnthcall) made their first
contribution in
[https://github.com/moq/moq/pull/1235](https://togithub.com/moq/moq/pull/1235)
- [@&#8203;wismann](https://togithub.com/wismann) made their first
contribution in
[https://github.com/moq/moq/pull/1322](https://togithub.com/moq/moq/pull/1322)
- [@&#8203;david-kalbermatten](https://togithub.com/david-kalbermatten)
made their first contribution in
[https://github.com/moq/moq/pull/1346](https://togithub.com/moq/moq/pull/1346)
- [@&#8203;dependabot](https://togithub.com/dependabot) made their first
contribution in
[https://github.com/moq/moq/pull/1360](https://togithub.com/moq/moq/pull/1360)

**Full Changelog**: moq/moq.spikes@v4.18.4...v4.20.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/Testably/Testably.Abstractions).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNy4xIiwidXBkYXRlZEluVmVyIjoiMzYuMjcuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@stakx
Copy link
Contributor Author

stakx commented Aug 10, 2023

@andy1547, sorry for not replying earlier, I've been somewhat busy with my daytime life lately. Fortunately, @kzu has in the meantime cleared the road-blocks that have prevented us from doing releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants