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

I have a unit test where I have to mock a non-virtual method that returns a bool type public class XmlCupboardAccess { public bool IsDataEntityInXmlCupboard(string dataId, out string nameInCupboard, out string refTypeInCupboard, string nameTemplate = null) { return IsDataEntityInXmlCupboard(_theDb, dataId, out nameInCupboard, out refTypeInCupboard, nameTemplate); } } So I have a mock object of XmlCupboardAccess … Read more

How to unit test with ILogger in ASP.NET Core

This is my controller: public class BlogController : Controller { private IDAO<Blog> _blogDAO; private readonly ILogger<BlogController> _logger; public BlogController(ILogger<BlogController> logger, IDAO<Blog> blogDAO) { this._blogDAO = blogDAO; this._logger = logger; } public IActionResult Index() { var blogs = this._blogDAO.GetMany(); this._logger.LogInformation(“Index page say hello”, new object[0]); return View(blogs); } } As you can see I have 2 … Read more

Verify a method call using Moq

I am fairly new to unit testing in C# and learning to use Moq. Below is the class that I am trying to test. class MyClass { SomeClass someClass; public MyClass(SomeClass someClass) { this.someClass = someClass; } public void MyMethod(string method) { method = “test” someClass.DoSomething(method); } } class Someclass { public DoSomething(string method) { … Read more

Verifying a specific parameter with Moq

public void SubmitMessagesToQueue_OneMessage_SubmitSuccessfully() { var messageServiceClientMock = new Mock<IMessageServiceClient>(); var queueableMessage = CreateSingleQueueableMessage(); var message = queueableMessage[0]; var xml = QueueableMessageAsXml(queueableMessage); messageServiceClientMock.Setup(proxy => proxy.SubmitMessage(xml)).Verifiable(); //messageServiceClientMock.Setup(proxy => proxy.SubmitMessage(It.IsAny<XmlElement>())).Verifiable(); var serviceProxyFactoryStub = new Mock<IMessageServiceClientFactory>(); serviceProxyFactoryStub.Setup(proxyFactory => proxyFactory.CreateProxy()).Returns(essageServiceClientMock.Object); var loggerStub = new Mock<ILogger>(); var client = new MessageClient(serviceProxyFactoryStub.Object, loggerStub.Object); client.SubmitMessagesToQueue(new List<IMessageRequestDTO> {message}); //messageServiceClientMock.Verify(proxy => proxy.SubmitMessage(xml), Times.Once()); messageServiceClientMock.Verify(); } … Read more

Mocking Extension Methods with Moq

I have a preexisting Interface… public interface ISomeInterface { void SomeMethod(); } and I’ve extended this intreface using a mixin… public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } I have a class thats calling this which I want to test… public class Caller { private readonly … Read more

Different return values the first and second time with Moq

I have a test like this: [TestCase(“~/page/myaction”)] public void Page_With_Custom_Action(string path) { // Arrange var pathData = new Mock<IPathData>(); var pageModel = new Mock<IPageModel>(); var repository = new Mock<IPageRepository>(); var mapper = new Mock<IControllerMapper>(); var container = new Mock<IContainer>(); container.Setup(x => x.GetInstance<IPageRepository>()).Returns(repository.Object); repository.Setup(x => x.GetPageByUrl<IPageModel>(path)).Returns(() => pageModel.Object); pathData.Setup(x => x.Action).Returns(“myaction”); pathData.Setup(x => x.Controller).Returns(“page”); var resolver … Read more