'Moq setup for generic method - cannot be used as type parameter T
I'm trying to mock this method.
Task<Response> SendTemplatedEmail<T>(TemplatedEmail<T> templatedEmail, EmailRecipient<T> recipient)
where T : TemplateData;
This is the closest I've got that makes logical sense to me.
this.mockSender.Setup(x => x.SendTemplatedEmail<It.IsSubtype<TemplateData>>(It.IsAny<It.IsSubtype<TemplateData>>, It.IsAny<It.IsSubtype<TemplateData>>));
But I get the following error:
CS0311: The type TemplatedEmail<Moq.It.Is.IsSubtype<TemplateData>> cannot be used as type parameter 'T' in the generic type or method IEmailSender.SendTemplatedEmail<T> ( TemplatedEmail<T>, EmailRecipient<T>>)' There is no implicit reference conversion from 'TemplateEmail<Moq.It.IsSubtype<TemplateData>> to TemplateData)
Here are the relevant class.
public class TemplatedEmail<T>
where T : TemplateData
{ ... }
In plain English, the method takes a templatedEmail parameter of type TemplatedEmail<T> where T is a subclass of TemplateData. It also takes second generic parameter EmailRecipient of the same type T.
Solution 1:[1]
this.mockSender.Setup(
x => x.SendTemplatedEmail<IsTemplateData<TemplateData>>(
It.IsAny<TemplatedEmail<IsTemplateData<TemplateData>>>(),
It.IsAny<EmailRecipient<IsTemplateData<TemplateData>>>()));
From: https://www.damirscorner.com/blog/posts/20200605-MatchingGenericTypeArgumentsWithMoq.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dan Cook |
