Mocking generic methods in Moq without specifying T

In Moq 4.13 they introduced the It.IsAnyType type which you can using to mock generic methods. E.g. public interface IFoo { bool M1<T>(); bool M2<T>(T arg); } var mock = new Mock<IFoo>(); // matches any type argument: mock.Setup(m => m.M1<It.IsAnyType>()).Returns(true); // matches only type arguments that are subtypes of / implement T: mock.Setup(m => m.M1<It.IsSubtype<T>>()).Returns(true); … Read more

Mocking a method to throw an exception (moq), but otherwise act like the mocked object?

Here’s how you can mock your FileConnection Mock<IFileConnection> fileConnection = new Mock<IFileConnection>( MockBehavior.Strict); fileConnection.Setup(item => item.Get(It.IsAny<string>,It.IsAny<string>)) .Throws(new IOException()); Then instantiate your Transfer class and use the mock in your method call Transfer transfer = new Transfer(); transfer.GetFile(fileConnection.Object, someRemoteFilename, someLocalFileName); Update: First of all you have to mock your dependencies only, not the class you are … Read more

MOQ – how to mock an interface that needs to be cast to another interface?

The way I understand you, you want to create a mock that implements two interfaces. With Moq, that is as simple as this: var mock = new Mock<IFoo>(); // Creates a mock from IFoo mock.As<IBar>(); // Adds IBar to the mock mock.As<IBar>().Setup(m => m.BarMethod()).Returns(new object()); // For setups. Now, you can set up expectations 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

Multiple Moq It.Is() Matching Arguments

Isn’t it confusing? You are trying to mock GetUser method but you set the Returns for that function’s return value’s property. You also want to state return type’s property based on mocked method. Here’s a way a more clear way: mockMembershipService.Setup(x => x.GetUser(It.IsAny<string>()) .Returns<string>(GetMembershipUser); Here’s a method to create the membership mock: private MembershipUser GetMembershipUser(string … Read more

moq objects Returns method, should return a null object

You don’t indicate what the error was, but this should work: unitOfWork.Setup(m => m.PhysicalTests).Returns((IRepository<PhysicalTest>)null); I suspect you tried to call it with Returns(null), which causes the compiler to complain since Returns is overloaded and it doesn’t know which method should be called. Casting to a specific type removes the ambiguity.

How to mock ModelState.IsValid using the Moq framework?

You don’t need to mock it. If you already have a controller you can add a model state error when initializing your test: // arrange _controllerUnderTest.ModelState.AddModelError(“key”, “error message”); // act // Now call the controller action and it will // enter the (!ModelState.IsValid) condition var actual = _controllerUnderTest.Index();

Using Moq to mock only some methods

This is called a partial mock, and the way I know to do it in Moq requires mocking the class rather than the interface and then setting the “Callbase” property on your mocked object to “true”. This will require making all the methods and properties of the class you are testing virtual. Assuming this isn’t … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)