简体   繁体   中英

How can you mock a method that returns an interface?

I need to mock a method that returns an interface and I can't figure it out. This was my try. I don't know how what to return, as you can not return an interface.

        messagingClient.Setup(x =>
             x.CreateMessage(new OrganizationUserDeletedDomainEvent
             {
                 UserType = 1,
                 OrganizationUserId = 1,
                 OrganizationId = 1,
                 DeletedAt = 100000
             })).Returns();

The method has this signature:

IMessage<TContent> CreateMessage<TContent>(TContent messageContent)
   where TContent : class, new();

you can create a mock of IMessage and use this mock to be returned

var messageMock = new Mock<IMessage<OrganizationUserDeletedDomainEvent>>();

messagingClient.Setup(x =>
     x.CreateMessage(new OrganizationUserDeletedDomainEvent
     {
         UserType = 1,
         OrganizationUserId = 1,
         OrganizationId = 1,
         DeletedAt = 100000
     })).Returns(messageMock.Object);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM