How can I tell Moq to return a Task?

Your method doesn’t have any callbacks so there is no reason to use .CallBack(). You can simply return a Task with the desired values using .Returns() and Task.FromResult, e.g.: MyType someValue=…; mock.Setup(arg=>arg.DoSomethingAsync()) .Returns(Task.FromResult(someValue)); Update 2014-06-22 Moq 4.2 has two new extension methods to assist with this. mock.Setup(arg=>arg.DoSomethingAsync()) .ReturnsAsync(someValue); mock.Setup(arg=>arg.DoSomethingAsync()) .ThrowsAsync(new InvalidOperationException()); Update 2016-05-05 As Seth … Read more

How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

The HttpClientFactory is derived from IHttpClientFactory Interface So it is just a matter of creating a mock of the interface var mockFactory = new Mock<IHttpClientFactory>(); Depending on what you need the client for, you would then need to setup the mock to return a HttpClient for the test. This however requires an actual HttpClient. var … Read more

Mock static property with moq

Moq can’t fake static members. As a solution you can create a wrapper class (Adapter Pattern) holding the static property and fake its members. For example: public class HttpRuntimeWrapper { public virtual string AppDomainAppVirtualPath { get { return HttpRuntime.AppDomainAppVirtualPath; } } } In the production code you can access this class instead of HttpRuntime and … Read more

How to mock static methods in c# using MOQ framework?

Moq (and other DynamicProxy-based mocking frameworks) are unable to mock anything that is not a virtual or abstract method. Sealed/static classes/methods can only be faked with Profiler API based tools, like Typemock (commercial) or Microsoft Moles (free, known as Fakes in Visual Studio 2012 Ultimate /2013 /2015). Alternatively, you could refactor your design to abstract … Read more

Mocking Static Methods

@Pure.Krome: good response but I will add a few details @Kevin: You have to choose a solution depending on the changes that you can bring to the code. If you can change it, some dependency injection make the code more testable. If you can’t, you need a good isolation. With free mocking framework (Moq, RhinoMocks, … Read more

Mocking Asp.net-mvc Controller Context

Using MoQ it looks something like this: var request = new Mock<HttpRequestBase>(); request.Expect(r => r.HttpMethod).Returns(“GET”); var mockHttpContext = new Mock<HttpContextBase>(); mockHttpContext.Expect(c => c.Request).Returns(request.Object); var controllerContext = new ControllerContext(mockHttpContext.Object , new RouteData(), new Mock<ControllerBase>().Object); I think the Rhino Mocks syntax is similar.

How to mock the Request on Controller in ASP.Net MVC?

Using Moq: var request = new Mock<HttpRequestBase>(); // Not working – IsAjaxRequest() is static extension method and cannot be mocked // request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */); // use this request.SetupGet(x => x.Headers).Returns( new System.Net.WebHeaderCollection { {“X-Requested-With”, “XMLHttpRequest”} }); var context = new Mock<HttpContextBase>(); context.SetupGet(x => x.Request).Returns(request.Object); var controller = new YourController(); controller.ControllerContext = … Read more

How to mock an async repository with Entity Framework Core

Thanks to @Nkosi for pointing me to a link with an example of doing the same thing in EF 6: https://msdn.microsoft.com/en-us/library/dn314429.aspx. This didn’t work exactly as-is with EF Core, but I was able to start with it and make modifications to get it working. Below are the test classes that I created to “mock” IAsyncQueryProvider: … Read more

How to mock non virtual methods?

Moq cannot mock non virtual methods on classes. Either use other mocking frameworks such as Type mock Isolator which actually weaves IL into your assembly or place an interface on EmailService and mock that.