How to mock ConfigurationManager.AppSettings with moq

I am using AspnetMvc4. A moment ago I wrote ConfigurationManager.AppSettings[“mykey”] = “myvalue”; in my test method and it worked perfectly. Explanation: the test method runs in a context with app settings taken from, typically a web.config or myapp.config. ConfigurationsManager can reach this application-global object and manipulate it. Though: If you have a test runner running … Read more

How to unit test with ILogger in ASP.NET Core

Just mock it as well as any other dependency: var mock = new Mock<ILogger<BlogController>>(); ILogger<BlogController> logger = mock.Object; //or use this short equivalent logger = Mock.Of<ILogger<BlogController>>() var controller = new BlogController(logger); You probably will need to install Microsoft.Extensions.Logging.Abstractions package to use ILogger<T>. Moreover you can create a real logger: var serviceProvider = new ServiceCollection() .AddLogging() … Read more

Moq + Unit Testing – System.Reflection.TargetParameterCountException: Parameter count mismatch

It’s your Returns clause. You have a 4 parameter method that you’re setting up, but you’re only using a 1 parameter lambda. I ran the following without issue: [TestMethod] public void IValueConverter() { var myStub = new Mock<IValueConverter>(); myStub.Setup(conv => conv.Convert(It.IsAny<object>(), It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<CultureInfo>())). Returns((object one, Type two, object three, CultureInfo four) => (int)one + … Read more

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

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

How do you mock the session object collection using Moq

I started with Scott Hanselman’s MVCMockHelper, added a small class and made the modifications shown below to allow the controller to use Session normally and the unit test to verify the values that were set by the controller. /// <summary> /// A Class to allow simulation of SessionObject /// </summary> public class MockHttpSession : HttpSessionStateBase … 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