How to mock HttpRequestMessage with Moq

How to mock HttpRequestMessage with Moq

I'm working on a web Api. It's an ASP.NET application. I need to create a mock for the method CreateResponse(HttpStatusCode statusCode, T value) of HttpRequestMessage class. But every time I execute my test and get the exception

I'm working on a web Api. It's an ASP.NET application. I need to create a mock for the method CreateResponse(HttpStatusCode statusCode, T value) of HttpRequestMessage class. But every time I execute my test and get the exception

Test Name:  RHT.Tests.Controllers.AccountControllerTests.Can_login_with_valid_credentials
Test FullName:  RHT.Tests.Controllers.AccountControllerTests.Can_login_with_valid_credentials
Test Source:    Controllers\AccountControllerTests.cs : line 23
Test Outcome:   Failed
Test Duration:  0:00:01.028

Result StackTrace:
at Moq.Mock.ThrowIfSetupExpressionInvolvesUnsupportedMember(Expression setup, MethodInfo method) in C:\projects\moq4\src\Moq\Mock.cs:line 755 at Moq.Mock.SetupNonVoidPexProtected(Mock mock, LambdaExpression expression, Condition condition) in C:\projects\moq4\src\Moq\Mock.cs:line 433 at Moq.PexProtector.Invoke[T1,T2,T3,TResult](Func4 function, T1 arg1, T2 arg2, T3 arg3) in C:\projects\moq4\src\Moq\PexProtector.cs:line 38 at Moq.Mock.SetupNonVoid(Mock mock, LambdaExpression expression, Condition condition) in C:\projects\moq4\src\Moq\Mock.cs:line 421 at Moq.Mock1.Setup[TResult](Expression`1 expression) in C:\projects\moq4\src\Moq\Mock.Generic.cs:line 317 at RHT.Tests.Controllers.AccountControllerTests.Can_login_with_valid_credentials() in C:\Users\ttong\source\repos\RedHawkTestPlan\RHT.Tests\Controllers\AccountControllerTests.cs:line 51 Result Message: System.NotSupportedException : Invalid setup on an extension method: m => m.CreateResponse<string>(HttpStatusCode.OK, "value")

The test

        Mock<IAuthProvider> auth = new Mock<IAuthProvider>();
        auth.Setup(m => m.CreateEncryptedCookieValue(
            It.IsAny<bool>(), It.IsAny<string>()))
            .Returns("my_ticket");

    Mock&lt;HttpRequestMessage&gt; req = new Mock&lt;HttpRequestMessage&gt;();
    req.Setup(m =&gt; m.CreateResponse(
        HttpStatusCode.OK, "value"))
        .Returns(new HttpResponseMessage(HttpStatusCode.OK));

The test method has 3 mock objects. But only the HttpRequestMessage is fail to initialize. Any idea why it doesn't work?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Add asp.net Identity in empty project

LIKE | COMMENT | SHARE | SUBSCRIBE Today, in this tutorial you will see the process of adding an ASP.NET Identity to the ASP.NET Web Application developed in...

List all users from asp net core identity

LIKE | COMMENT | SHARE | SUBSCRIBE In this video, i will show you How to retrieve and display all the registered application users in asp.net core using the ...

Extend IdentityUser in ASP NET Core

LIKE | COMMENT | SHARE | SUBSCRIBE In this tutorials, I will show you how to extend IdentityUser in asp.net core.. Subscribe & Stay Connected! Thank You! ♥ #...