3

I'm trying to mock test with an asynchronous method but i don't know how to set up the moq test to allow it.

noSQLProvider.Setup(x => x.CreateDocumentAsync(It.IsIn<Uri>(), It.IsAny<object>())).Returns();

the error is telling me:

Usage: ResourceResponse x = await CreatDocumentAsync(...);

an expression tree may not contain a call or invocation that uses optional arguments.

Liam
  • 27,717
  • 28
  • 128
  • 190
Callum
  • 139
  • 1
  • 13
  • 1
    Possible duplicate of [How do I Moq a method that has an optional argument in its signature without explicitly specifying it or using an overload?](http://stackoverflow.com/questions/12957537/how-do-i-moq-a-method-that-has-an-optional-argument-in-its-signature-without-exp) – Liam Jul 05 '16 at 10:44

1 Answers1

4

I'm guessing you're using this?

In which case the error message is telling you quite clearly that that method has optional parameters and you need to be explicit about them:

noSQLProvider.Setup(x => x.CreateDocumentAsync(It.IsIn<Uri>(),
                                               It.IsAny<object>(),
                                               It.IsAny<RequestOptions>(),
                                               It.IsAny<bool>())).Returns();

This is a limitation that isn't related to async.

NB - I'm not sure on how It.IsAny comparison behaves for nulls, so you might have to replace It.IsAny<RequestOptions>() with something that explicitly sets up that parameter to be null (which is the default).

Ant P
  • 24,820
  • 5
  • 68
  • 105