James
James

Reputation: 2018

Moq - How to verify Action<T>?

I have the following code in my unit under test:

transaction.QueueCommand(x => x.AddItemToSet("key", "value"));

I want to Verify this was called using Moq (I love Moq).

I set up the following Verification in my unit test:

m_MockTransaction.Verify(x => x.QueueCommand(y => y.AddItemToSet("key", "value")));

Unfortunately this gives the following exception:

Exception thrown: 'System.NotSupportedException' in Moq.dll

Additional information: Unsupported expression: y => y.AddItemToSet("key", "value")

I understand this is because Moq cannot unwrap the delegate to figure out if the calls match or not. I wonder if someone has a method for working around this restriction, preferably not too nasty.

I have seen this answer https://stackoverflow.com/a/1120836/282090 which proposes converting a Func into a string, which can then be compared (this does not work with an Action. Unfortunately the code in my test will not be the same as in my unit under test.

For instance, I would do a verification for "key", "value" whilst the code may pass variables theKey, theValue.

Any assistance in ensuring I can verify the calls would be much appreciated.

Upvotes: 3

Views: 413

Answers (1)

James
James

Reputation: 2018

In this instance I found it was not worth it to figure out how to do something the Moq team had not achieved and instead turned this unit test into an integration test, verifying the correct data was in the database.

If someone has a better solution I can change this.

Upvotes: 1

Related Questions