This is a migrated thread and some comments may be shown as answers.

Mocking Static method call not working

1 Answer 199 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
David asked on 05 Feb 2015, 07:03 PM
I am not sure why my arrange call for a static method is trying to execute the original.

public void RunCompleteDivxJob_Negative_Exception()
    DebugView.IsTraceEnabled = true;
    var mediaId = 2000999;
    var mediaTypeId = (int)CmtMediaType.Video;
    var queueCmtId = 9999;
    var cmtFileType = CmtFileType.SourceMediaFile;
    var statusError = CmtQueueStatus.Error;
    var exception = new Exception("test");
    // ARRANGE
    var encodeJobStateMachineManager = Mock.Create<EncodeJobStateMachineManager>(Behavior.CallOriginal);
    var logger = Mock.Create<Logger>(Behavior.CallOriginal, typeof(EncodeJobStateMachineManager));
    var inst = new PrivateAccessor(encodeJobStateMachineManager);
    inst.SetProperty("_log", logger);
    var createCompleteJobCalled = false;
    Mock.Arrange(() => DivxEncodeJob.CreateCompleteJob(mediaId, mediaTypeId, queueCmtId, cmtFileType))
        .DoInstead(() => createCompleteJobCalled = true);
    Mock.Arrange(() => encodeJobStateMachineManager.EncodeJob.Submit()).Throws(exception).MustBeCalled();
    logger.Arrange(x => x.Error(Arg.AnyString, exception)).MustBeCalled();
    var updateQueueStatusCalled = false;
    Mock.Arrange(() => QueueDAO.UpdateQueueStatus(queueCmtId, statusError)).DoInstead(() =>  updateQueueStatusCalled = true);
    // ACT
    encodeJobStateMachineManager.RunCompleteDivxJob(mediaId, mediaTypeId, queueCmtId, cmtFileType);
    // ASSERT

The first static call is mocking the method call correctly:
Mock.Arrange(() => DivxEncodeJob.CreateCompleteJob(mediaId, mediaTypeId, queueCmtId, cmtFileType))
        .DoInstead(() => createCompleteJobCalled = true);

But the second static call is executing the original code:
Mock.Arrange(() => QueueDAO.UpdateQueueStatus(queueCmtId, statusError)).DoInstead(() => updateQueueStatusCalled = true);

These are essentially called exactly the same.  So why is the first one working as expected and the second not?

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 10 Feb 2015, 10:36 AM
Hi David,

The likely problem is that QueueDAO.UpdateQueueStatus is not called with the expected arguments. Check if adding the .IgnoreArguments() clause to its arrangement will make it work. If yes, then it's a problem with the arguments.

Use the debugger to double-check exactly what arguments the method is called with. Alternatively you can watch DebugView.FullTrace in the debugger, browse down to the interception of the QueueDAO.UpdateQueueStatus call and see what arguments it's called with and why no arrangement was chosen for the call.

In any case, I cannot tell exactly what's wrong with the test without seeing the code of the system under test. If the above doesn't help you, then consider sending me a repro project demonstrating the issue.


Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

General Discussions
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question