How to Mock (with Moq) Unity methods

后端 未结 4 1180
别跟我提以往
别跟我提以往 2021-01-04 04:04

Extension methods are not good for testing (that\'s described here: Mocking Extension Methods with Moq, http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomo

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-04 04:41

    Guess, I found most appropriate solution for test: It is not necessary to mock unity container and check if 'log' object was taken from it. I will just make a mock for 'Log' object, register its object instance in the container and check in test if this log object is really used.

    This will do what is required.

            Mock mockLog = new Mock();
            mockLog.Setup(mock=>mock.Id).Returns(TestLogId);
    
            IUnityContainer container = new UnityContainer();
            container
                .RegisterInstance(mockCommandExecutionLog.Object)
                ...
                ;
    
            ...
    
            mockLog.Verify(
                mock => mock.Id,
                Times.Once(),
                "It seems like 'Log' object is not used"
                );
    

    Thanks.

提交回复
热议问题