2

I've got a Moq mock of a class for which I need to verify whether a certain method was called. Depending on the type of a variable, I need to check if the method was called once or never.

So, this works:

if (exception is ValidationException)
    mockRequestHandler.Verify(x => x.HandleException(exception), 
    Times.Once);
else
    mockRequestHandler.Verify(x => x.HandleException(exception), 
    Times.Never);

I'm trying to use a ternary operator as follows, but it doesn't seem to work:

mockRequestHandler.Verify(x => x.HandleException(exception),
    (exception is ValidationException) ? Times.Once: Times.Never);

I get the following compile-time error:

Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'.

Is there something simple i'm overlooking or can the ternary operator not be used in this way?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ash
  • 5,786
  • 5
  • 22
  • 42
  • 1
    Why not put the exception check first? E.g. `(exception is ValidationException) ? mockRequestHandler.Verify(x => x.HandleException(exception), Times.Once) : mockRequestHandler.Verify(x => x.HandleException(exception), Times.Never);`. It does seem almost as long as your original, but take reference from this link: [Using conditional (?:) operator for method selection in C# (3.0)?](http://stackoverflow.com/questions/5186394/using-conditional-operator-for-method-selection-in-c-sharp-3-0) – Keyur PATEL Sep 15 '16 at 04:26
  • 2
    Could you please specify the type signatures for `Verify`, `Times.Once` and `Times.Never`? – Curtis Lusmore Sep 15 '16 at 04:28
  • You should be able to cast one of the operands to Func to resolve it – Alexei Levenkov Sep 15 '16 at 04:28

1 Answers1

8

As I can see in this source file, Times.Once and Times.Never are actually static methods, but not properties.

In order to verify that method is called once or is never called, you need to use it this way:

mockRequestHandler.Verify(x => x.HandleException(exception), Times.Once());
mockRequestHandler.Verify(x => x.HandleException(exception), Times.Never());

So, using a ternary operator it will be:

mockRequestHandler.Verify(x => x.HandleException(exception), 
    (exception is ValidationException) ? Times.Once() : Times.Never());
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • This is likely wrong suggestion for moq case. Check type of second parameter of verify to confirm – Alexei Levenkov Sep 15 '16 at 04:26
  • 1
    [verified on line 28](https://github.com/moq/moq4/blob/756bf4e3e7b213c6d819ae7acd5e04280a07a040/UnitTests/ExtensionsFixture.cs) – txtechhelp Sep 15 '16 at 04:31
  • 1
    @AlexeiLevenkov Thanks for suggestion. I did never use Moq, but I have seen this usage on GitHub, and [here](http://stackoverflow.com/questions/4206193/how-do-i-verify-a-method-was-called-exactly-once-with-moq). If OP tries this out and it won't work, I will remove my answer :) – Yeldar Kurmangaliyev Sep 15 '16 at 04:34
  • Firstly, thanks, this answer worked. However, my original usage `mockRequestHandler.Verify(x => x.HandleException(exception), Times.Once);` (method group....without the brackets) works fine. It also works when called as a method (with the brackets). – Ash Sep 15 '16 at 04:36
  • Thanks for checking - it since original sample showed method group (as delegate) and not the call I was not sure if both variants are supported. – Alexei Levenkov Sep 15 '16 at 05:19
  • 1
    @AlexeiLevenkov In [this source file](https://github.com/moq/moq4/blob/8594b26159bee2b698995cc7d70617a8a90e12ba/Source/Mock.Generic.cs) you see that the two syntaxes are allowed by two overloads. In fact, one calls the other: `public void Verify(Expression> expression, Times times) { /* ... */ }` and then `public void Verify(Expression> expression, Func times) { Verify(expression, times()); }`. – Jeppe Stig Nielsen Sep 17 '16 at 23:40