jeudi 26 novembre 2015

Moq - How to verify Action

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 http://ift.tt/1NeXtLQ 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.

Aucun commentaire:

Enregistrer un commentaire