moq icon indicating copy to clipboard operation
moq copied to clipboard

Verify something for each item in a list

Open Flatscha opened this issue 2 years ago • 3 comments

I am trying to write an extension method for Mock<T> to verify a call to every item in a list.

My extension: public static void VerifyMultiple<T, TItem>(this Mock<T> value, Action<T, TItem> action, Times times, IEnumerable<TItem> items) where T : class { foreach (var item in items) { value.Verify(x => action(x, item), times); } }

Example call: this._mockDAL.VerifyMultiple((x,i) => x.Save(i), Times.Once(), items);

But I get the error System.NotSupportedException: 'Unsupported expression: x'

I unfortunately do not know what is wrong with that call or why this exception is thrown Can somebody explain?

Back this issue Back this issue

Flatscha avatar Jul 27 '23 14:07 Flatscha

Make your action parameter an Expression<Action<T, TItem>> instead? That's the type Mock<T>.Verify receives and is that makes it possible to inspect which mock member invocation to verify.

kzu avatar Aug 04 '23 19:08 kzu

Unfortunately I already tried that and it doesn't change anything.

Flatscha avatar Aug 05 '23 07:08 Flatscha

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

github-actions[bot] avatar Aug 24 '24 20:08 github-actions[bot]

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

github-actions[bot] avatar Apr 04 '25 01:04 github-actions[bot]

This issue will now be closed since it has been labeled 'stale' without activity for 30 days.

github-actions[bot] avatar May 05 '25 01:05 github-actions[bot]