I tried to make a generic helper method using Moq with nunit as described below
public void PrepareWebRequest<T>() where T : new()
{
httpCommunicator = new Mock<IHttpCommunicator>();
httpCommunicator.Setup(x => x.Post(It.IsAny<Object>(),
It.IsAny<string>())).Throws<T>();
service = new ApiClient(httpCommunicator.Object);
}
But this causes the following error:
The type ‘T’ cannot be used as type parameter ‘TException’ in the generic type or method ‘Moq.Language.IThrows.Throws()’. There is no boxing conversion or type parameter conversion from ‘T’ to ‘System.Exception’.
I know this can be refactored by not using the generic method on Moq, but would really like to know what I’m doing wrong.
Best regards
Rasmus
Problem was a missing Exception in the Where clause. Should be