最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Issue with MoqSetup EmailClient for Azure Communication Service - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question edited Mar 16 at 13:21 Michał Turczyn 37.9k18 gold badges55 silver badges82 bronze badges asked Mar 15 at 0:34 dr.Xisdr.Xis 1252 gold badges4 silver badges11 bronze badges 2
  • 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
Add a comment  | 

2 Answers 2

Reset to default 1

It'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>()))

发布评论

评论列表(0)

  1. 暂无评论