I'm trying to mock and setup a XUnit test for my service when calling Azure EmailClient for both SendAsync
and Send
.
var EmailClientClientMock = new Mock<EmailClient>();
var emailSendOperationMock = new Mock<EmailSendOperation>();
EmailSendOperation emailSendOperation = new EmailSendOperation("random", EmailClientClientMock.Object);
var emailMessage = new Mock<EmailMessage>();
_mockEmailClient
.Setup(client => client.SendAsync(
It.Is<WaitUntil>(w => w == Azure.WaitUntil.Started),
It.IsAny<EmailMessage>()
))
.ReturnsAsync(mockResult);
But I'm having this issue - I don't know how to solve it:
An expression tree may not contain a call or invocation that uses optional arguments
Is wrapping this EmailClient
in an interface an option?
I'm trying to mock and setup a XUnit test for my service when calling Azure EmailClient for both SendAsync
and Send
.
var EmailClientClientMock = new Mock<EmailClient>();
var emailSendOperationMock = new Mock<EmailSendOperation>();
EmailSendOperation emailSendOperation = new EmailSendOperation("random", EmailClientClientMock.Object);
var emailMessage = new Mock<EmailMessage>();
_mockEmailClient
.Setup(client => client.SendAsync(
It.Is<WaitUntil>(w => w == Azure.WaitUntil.Started),
It.IsAny<EmailMessage>()
))
.ReturnsAsync(mockResult);
But I'm having this issue - I don't know how to solve it:
An expression tree may not contain a call or invocation that uses optional arguments
Is wrapping this EmailClient
in an interface an option?
- This question is similar to: An expression tree may not contain a call or invocation that uses optional arguments when mocking a function to return an object. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Ivan Petrov Commented Mar 15 at 0:47
- is the same doubt, no doubt - but for some reason i can't put it to work. plus. – dr.Xis Commented Mar 15 at 1:00
2 Answers
Reset to default 1It's strange error message, but what you are missing is last parameter for mocked method SendAsync
, which is CancellationToken
:
var EmailClientClientMock = new Mock<EmailClient>();
var emailSendOperationMock = new Mock<EmailSendOperation>();
var emailSendOperation = new EmailSendOperation("random", EmailClientClientMock.Object);
var emailMessage = new Mock<EmailMessage>();
EmailClientClientMock
.Setup(client => client.SendAsync(
It.Is<WaitUntil>(w => w == WaitUntil.Started),
It.IsAny<EmailMessage>(),
It.IsAny<CancellationToken>()
))
.ReturnsAsync(emailSendOperation);
it was actually very easy - and the answer was actually in the description - the optional part regard the cancelation token:
emailClientMock.Setup(c => c.SendAsync(WaitUntil.Started, It.IsAny<EmailMessage>(), It.IsAny<CancellationToken>()))