Using Moq to determine if a method is called

You can see if a method in something you have mocked has been called by using Verify, e.g.: static void Main(string[] args) { Mock<ITest> mock = new Mock<ITest>(); ClassBeingTested testedClass = new ClassBeingTested(); testedClass.WorkMethod(mock.Object); mock.Verify(m => m.MethodToCheckIfCalled()); } class ClassBeingTested { public void WorkMethod(ITest test) { //test.MethodToCheckIfCalled(); } } public interface ITest { void MethodToCheckIfCalled(); … Read more

Using Moq to mock an asynchronous method for a unit test

You’re creating a task but never starting it, so it’s never completing. However, don’t just start the task – instead, change to using Task.FromResult<TResult> which will give you a task which has already completed: … .Returns(Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK))); Note that you won’t be testing the actual asynchrony this way – if you want to do that, … Read more

Why am I getting an Exception with the message “Invalid setup on a non-virtual (overridable in VB) member…”?

Moq cannot mock non-virtual methods and sealed classes. While running a test using mock object, MOQ actually creates an in-memory proxy type which inherits from your “XmlCupboardAccess” and overrides the behaviors that you have set up in the “SetUp” method. And as you know in C#, you can override something only if it is marked … Read more

Mocking HttpContextBase with Moq

I’m using a version of some code Steve Sanderson included in his Pro Asp.NET MVC book… and I’m currently having a moral dilemma whether it’s okay to post the code here. How about I compromise with a highly stripped down version? 😉 So this can easily be reused, create a class similar to the one … Read more

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 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

To mock an object, does it have to be either implementing an interface or marked virtual?

To mock a type, it must either be an interface (this is also called being pure virtual) or have virtual members (abstract members are also virtual). By this definition, you can mock everything which is virtual. Essentially, dynamic mocks don’t do anything you couldn’t do by hand. Let’s say you are programming against an interface … 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

tech