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: How to get to a parameter passed to a method of a mocked service

You can use the Mock.Callback-method: var mock = new Mock<Handler>(); SomeResponse result = null; mock.Setup(h => h.AsyncHandle(It.IsAny<SomeResponse>())) .Callback<SomeResponse>(r => result = r); // do your test new Foo(mock.Object).Bar(22); Assert.NotNull(result); If you only want to check something simple on the passed in argument, you also can do it directly: mock.Setup(h => h.AsyncHandle(It.Is<SomeResponse>(response => response != null)));

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

Different return values the first and second time with Moq

With the latest version of Moq(4.2.1312.1622), you can setup a sequence of events using SetupSequence. Here’s an example: _mockClient.SetupSequence(m => m.Connect(It.IsAny<String>(), It.IsAny<int>(), It.IsAny<int>())) .Throws(new SocketException()) .Throws(new SocketException()) .Returns(true) .Throws(new SocketException()) .Returns(true); Calling connect will only be successful on the third and fifth attempt otherwise an exception will be thrown. So for your example it would … Read more

Assigning out/ref parameters in Moq

For ‘out’, the following seems to work for me. public interface IService { void DoSomething(out string a); } [TestMethod] public void Test() { var service = new Mock<IService>(); var expectedValue = “value”; service.Setup(s => s.DoSomething(out expectedValue)); string actualValue; service.Object.DoSomething(out actualValue); Assert.AreEqual(expectedValue, actualValue); } I’m guessing that Moq looks at the value of ‘expectedValue’ when you … 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

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